由于window和linux默认字符编码不同,所以某些与中文相关的操作,特别是xml数据,需要转码以适应不同操作系统。
工具类:
/**
* @param infile 源文件路径
* @param outfile 输出文件路径
* @param from 源文件编码
* @param to 目标文件编码
* @throws IOException
* @throws UnsupportedEncodingException
*/
public static void convert(String infile, String outfile, String from, String to)
{
try {
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(infile), from));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outfile), to)));
String reading;
while ((reading = in.readLine()) != null)
{
out.println(reading);
}
out.close();
in.close();
} catch (UnsupportedEncodingException e) {
System.out.println("不支持的编码格式---"+e.getMessage());
} catch (FileNotFoundException e) {
System.out.println("文件不存在---"+e.getMessage());
} catch (IOException e) {
System.out.println("IO异常---"+e.getMessage());
}
}