Java——利用转换流将GBK格式文件以UTF-8输出
程序员文章站
2022-03-31 10:25:33
...
利用转换流将GBK格式文件以UTF-8输出
实现代码:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class HomeWork2 {
public static void main(String[] args) throws IOException {
GBKtoUTF("D:\\360MoveData\\Users\\xucc\\Desktop\\h.cpp", "D:\\\\360MoveData\\\\Users\\\\xucc\\\\Desktop\\\\h2.cpp");
}
public static void GBKtoUTF(String file1,String file2) throws IOException {
//1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称GBK
InputStreamReader isr = new InputStreamReader(new FileInputStream(file1),"GBK");
//2.创建OutputTreamWrite对象,构造方法中传递字节输出流和指定的编码表名称UTF-8
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file2),"utf-8");
int len = 0;
while((len = isr.read()) != -1) {
osw.write(len);
}
//3.释放资源
isr.close();
osw.close();
}
}