用JAVA语言编写程序:文件一的内容复制到文件二,并且复制过去后小写变成大写

如题所述

package test1;import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;public class Test1 {
public static void main(String[] args) throws IOException {
String filename="d:\\1.txt";
BufferedReader br = new BufferedReader(new FileReader(new File(filename)));
String distname="d:\\2.txt";
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(distname)));
String line = null;
while(true){
line = br.readLine();
if(line==null){
break;
}
char[] chars = line.toCharArray();
StringBuffer sb = new StringBuffer();
for(int i=0;i<chars.length;i++){
if(Character.isLowerCase(chars[i])){
sb.append(Character.toUpperCase(chars[i]));
}else{
sb.append(chars[i]);
}
}
line = sb.toString();
bw.write(line+"\r\n");
}
br.close();
bw.close();
}}
温馨提示:答案为网友推荐,仅供参考
第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);
}
}
第2个回答  2013-08-22
CD
相似回答