版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yuming226/article/details/84496396
1、FileReader是使用默认码表读取文件,如果需要使用指定码表读取,那么可以使用InputStreamReader(字节流,编码表)。
2、FileWriter是使用默认码表写出文件,如果需要使用指定码表写出,那么可以使用OutputStreamWriter(字节流,编码表)。
代码演示如下:
package zifu;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Demo8_TransIO {
public static void main(String[] args) throws IOException {
/*FileInputStream fis = new FileInputStream("gbk.txt");
FileOutputStream fos = new FileOutputStream("utf8.txt");*/
InputStreamReader fis = new InputStreamReader(new FileInputStream("gbk.txt"),"gbk");
OutputStreamWriter fos = new OutputStreamWriter(new FileOutputStream("utf8.txt"),"UTF-8");
int b;
while ((b = fis.read()) != -1) {
fos.write(b);
}
fis.close();
fos.close();
}
}