StringBuffer 读写文件
import java.io.;
public class FileIO_StringBuffer {
public static void main(String[] args) {
//读文件到Stringbuffer
StringBuffer sb = new StringBuffer();
String sourcePath = "C:\Users\Administrator\Desktop\1.txt";//源文件路径(.*)
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(sourcePath));
String str;
while((str = br.readLine()) != null) {//逐行读取
sb.append(str);//加在StringBuffer尾
sb.append("\r\n");//行尾 加换行符
}
br.close();//别忘记,切记
}
catch(Exception e){
System.out.println(e.getMessage());
}
//写入文件
String targetPath = "C:\Users\Administrator\Desktop\2.txt";//目标文件路径
File f = new File(targetPath);//新建文件
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
bw.write(sb.toString());
bw.flush();
bw.close();
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
原文地址:https://www.cnblogs.com/yocichen/p/9286676.html