关键点:用FileInputStream和FileOutputStream。
br = new BufferedReader(newInputStreamReader(new FileInputStream( srcFileName),srcEncoding));
bw = new BufferedWriter(newOutputStreamWriter(newFileOutputStream(destFileName), destEncoding));
例:
/**
* @param srcFileName 原文件的路径
* @param destFileName 转换后文件的路径
* @param srcEncoding 原文件的编码,如“GBK”
* @param destEncoding 目标编码,如“UTF-8”
*/
public boolean convertFile(String srcFileName,String destFileName,
String srcEncoding, String destEncoding) {
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(
srcFileName), srcEncoding));
bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(destFileName), destEncoding));
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
// System.out.println(line);
bw.newLine();
}
} catch (Exception e) {
} finally {
try {
if (br != null){
br.close();
}
if (bw != null){
bw.close();
}
return true;
} catch (IOException e) {
}
}
return false;
}