将一个文件由gbk 转成 utf-8
程序员文章站
2022-04-08 16:41:42
...
public void transform(String fileName) {
try {
String line_separator = System.getProperty("line.separator");
FileInputStream fis;
fis = new FileInputStream(fileName);
StringBuffer content = new StringBuffer();
DataInputStream in = new DataInputStream(fis);
BufferedReader d = new BufferedReader(new InputStreamReader(in,
"GBK"));// , "UTF-8"
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(fileName),
"utf-8");
ow.write(content.toString());
ow.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//或
//IOUtils来自org.apache.commons.io.IOUtils
public String handle(String gbkString) {
try {
return IOUtils.toString(IOUtils.toInputStream(gbkString, "UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}