有一批文件,文件编码方式有gbk,有gb2312,有utf-8,打算统一编码为utf-8.因此想用java编写一个程序读入指定文件夹的文件,然后将其转换为utf-8格式。
自己试着写了一个:
private static void transferFile(File file) throws IOException {
String line_separator = System.getProperty("line.separator");
FileInputStream fis = new FileInputStream(file);
StringBuffer content = new StringBuffer();
DataInputStream in = new DataInputStream(fis);
BufferedReader d = new BufferedReader(new InputStreamReader(in, "GBK"));
String line = null;
while ((line = d.readLine()) != null)
content.append(line + line_separator);
d.close();
in.close();
fis.close();
Writer ow = new OutputStreamWriter(new FileOutputStream(file), "utf-8");
ow.write(content.toString());
ow.close();
}
但是这种方法必须已知文件的编码方式才能处理。因为是批量处理,所以事先不知文件的编码方式,请问应采用何种方法来实现啊。这个问题困扰我很久了,谢谢大家了。
自己试着写了一个:
private static void transferFile(File file) throws IOException {
String line_separator = System.getProperty("line.separator");
FileInputStream fis = new FileInputStream(file);
StringBuffer content = new StringBuffer();
DataInputStream in = new DataInputStream(fis);
BufferedReader d = new BufferedReader(new InputStreamReader(in, "GBK"));
String line = null;
while ((line = d.readLine()) != null)
content.append(line + line_separator);
d.close();
in.close();
fis.close();
Writer ow = new OutputStreamWriter(new FileOutputStream(file), "utf-8");
ow.write(content.toString());
ow.close();
}
但是这种方法必须已知文件的编码方式才能处理。因为是批量处理,所以事先不知文件的编码方式,请问应采用何种方法来实现啊。这个问题困扰我很久了,谢谢大家了。
byte[] bs2=s.getBytes("GBK");
if(Arrays.equals(bs,bs2)){
//非常大的可能是GBK编码
}原理是: 不正确的编码转换会对信息造成不可逆的修改
另外: GBK是包含GB2312的
http://cpdetector.sourceforge.net/
public static byte[] toUTF8(byte[] bs){
try{
String s=new String(bs,"GBK");
byte[] bs2=s.getBytes("GBK");
if(Arrays.equals(bs,bs2)){
return s.getBytes("UTF-8");
}
}
catch(Exception ex){
}
return bs;
}