@SuppressWarnings("all")
public class CharStreamMax {
FileReader fileReader = null;
FileWriter fileWriter = null;
//打印文件内容
public void inPut() throws IOException {
File file = new File("D:\\IdeaProjects\\javase\\src\\test.txt");
fileReader = new FileReader(file);
char[] chars = new char[1024];
while (fileReader.read(chars) != -1) {
System.out.print(String.valueOf(chars));
}
System.out.println("\n");
fileReader.close();
}
//修改文件内容
public void outPut() throws IOException {
File file1 = new File("D:\\IdeaProjects\\javase\\src\\test.txt");
File file2 = new File("D:\\IdeaProjects\\javase\\src\\IOStream2");
//读取file2的内容
fileReader = new FileReader(file2);
//将file2的内容写入到file1中
fileWriter = new FileWriter(file1);
char[] chars = new char[1024];
int len = 0;
while ((len = fileReader.read(chars)) != -1) {
fileWriter.write(chars,0,len);
}
fileReader.close();
fileWriter.flush();
fileWriter.close();
}
public static void main(String[] args) throws IOException {
CharStreamMax charStreamMax = new CharStreamMax();
//打印文件内容
charStreamMax.inPut();
//修改文件内容
charStreamMax.outPut();
//再次打印文件内容,检测是否已修改
charStreamMax.inPut();
}
}