Convert GBK-encoded text files to UTF-8-encoded text files.
case study
- Specify the conversion stream of GBK encoding and read the text file.
- Use the UTF-8 encoded conversion stream to write out a text file.
Case realization
- Create an InputStreamReader object, pass the byte input stream and the specified encoding table name GBK in the constructor
- Create an OutputStreamWriter object, pass the byte output stream and the specified encoding table name UTF-8 in the construction method
- Use the method read in the InputStreamReader object to read the file
- Use the method write in the OutputStreamWriter object to write the read data to the file
- Release resources
package com.itheima.demo03.ReverseStream;
import java.io.*;
public class Demo04Test {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("GBK格式的文本.txt"), "GBK");
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("UTF-8格式的文本.txt"), "UTF-8");
int len = 0;
while ((len = isr.read()) != -1) {
osw.write(len);
}
osw.close();
isr.close();
}
}
operation result