还是先附上API文档
即FileReader和FileWriter是
InputStreamReader和OutputStreamWriter的子类直接使用即可
附上例子:
package ioTest;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class demo6 {
public static void main(String[] args) throws IOException{
FileReader fr = new FileReader("demo.txt");
FileWriter fw = new FileWriter("in.txt");
/* 方式一
int ch = 0;
while ((ch = fr.read()) != -1) {
fw.write(ch);
fw.flush();
}
//释放资源
fr.close();
fw.close();
*/
//方式二
char chs[] = new char[1024];
int len = 0;
len = len + 0;
while ((len = fr.read(chs)) != -1) {
fw.write(chs,0,len);
//不用osr.write(chs);
fw.flush();
}
fr.close();
fw.close();
}
}
参照上一篇博文用以对照
https://blog.csdn.net/weixin_43505709/article/details/96136385