第1个回答 2012-02-10
package test3;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* class名:CopyFile <BR>
* class说明:将源文件内容复制到目标文件,并且将其中小写的字母转换成大写字母 <BR>
* 备注:由于需要在复制文件的同时进行转换,所以无法使用缓存机制,复制效率较慢。
* @version <none>
* @author Jr
*
*/
public class CopyFile {
/**
* method名:copyAndConvert
* method说明:将源文件内容复制到目标文件,并且将其中小写的字母转换成大写字母
* @param <sourceFile> <源文件>
* @param <destinyFile> <目标文件>
* @throws <IOException> <当源文件不存在/找不到时将会抛出java.io.IOException>
*/
private static void copyAndConvert(String sourceFile, String destinyFile) throws IOException{
InputStream is = null;
OutputStream os = null;
int b;
try {
is = new FileInputStream(sourceFile);
os = new FileOutputStream(destinyFile);
while((b = is.read()) != -1){
if ((b >= 97) && (b <= 122)){ //a的ASCII码是97,z的ASCII码是122
b -= 32;
}
os.write(b);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
os.close();
is.close();
}
}
public static void main(String[] args) throws IOException{
String sourceFile = "d:/book1.txt";
String destinyFile = "d:/book2.txt";
copyAndConvert(sourceFile, destinyFile);
}
}