有个名叫fgbk的文件,里面有gbk格式的“abc中”
16进制为 61 62 63 D6 D0
现在需要复制转换成目标文件futf8
16进制为 61 62 63 E4 B8 AD
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Scanner;
public class fileCopyChangeCode {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("原文件");
String from = sc.nextLine();
System.out.println("目标文件");
String to = sc.nextLine();
System.out.println("原文件编码");
String fromCharset = sc.nextLine();
System.out.println("目标文件编码");
String toCharset = sc.nextLine();
try {
copy(from, to, fromCharset, toCharset);
System.out.println("复制转码完成!");
} catch (IOException e) {
e.printStackTrace();
}
sc.close();
}
private static void copy(String from, String to, String fromCharset, String toCharset) throws IOException {
InputStreamReader in = new InputStreamReader(new FileInputStream(from),fromCharset);
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(to),toCharset);
char buff[] =new char[8192];
int n;
while((n=in.read(buff))!=-1)
{
out.write(buff,0,n);
}
in.close();
out.close();
}
}
运行结果
原文件
fgbk
目标文件
futf8
原文件编码
gbk
目标文件编码
utf8
复制转码完成!