用java的流复制文件,并改变编码格式,由ansi改为utf-8格式
我使用java的流复制文件,字节或者字符流都可以,并想统一改变原来的ansi编码格式,由ansi改为复制后的utf-8编码格式,求帮助!!我试了下面的复制,但是复制出来的是乱码--------
/**
* 拷贝文件到指定位置
*
* @param srcFile
* @param destFile
*/
public static void copyFile(File srcFile, File destFile) {
BufferedReader buf = null;
PrintWriter pw = null;
try {
buf = new BufferedReader(new InputStreamReader(new FileInputStream(srcFile)));
pw = new PrintWriter(new FileOutputStream(destFile));
String str = null;
while((str = buf.readLine()) != null){
String outStr = new String(str.getBytes("ISO8859-1"),"UTF-8");
pw.write(outStr);
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
//关闭
try {
if (buf != null) {
buf.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if(pw != null){
try {
pw.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
} --------------------编程问答-------------------- 中文windows里notepad.exe里的ansi其实就是平台默认的gbk
BufferedReader reader = new BufferedReader(new FileReader("ccc.txt"));--------------------编程问答-------------------- 学习
PrintWriter writer = new PrintWriter("abc.txt", "UTF-8");
for(String line = reader.readLine(); line != null; line = reader.readLine()){
writer.println(line);
}
补充:Java , Java SE