今天随手做了复制文件的代码,发现了一个问题
public void readByPath() throws IOException{File file=new File("D:/lidahu/lidahu.doc");
File file2=new File("D:/lidahu/lidahu2.doc");
try {
FileInputStream fs=new FileInputStream(file);
FileOutputStream fo = new FileOutputStream(file2);
byte[] name=new byte[(int) file.length()];
if(fs.read(name)!=-1){
fo.write(name);
}
fs.close();
fo.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
上面代码是没有问题的
public void readByPath() throws IOException{
File file=new File("D:/lidahu/lidahu.doc");
File file2=new File("D:/lidahu/lidahu3.doc");
try {
FileReader fs=new FileReader(file);
FileWriter fo = new FileWriter(file2);
char[] name=new char[(int) file.length()+1];
if(fs.read(name)!=-1){
fo.write(name);
}
fs.close();
fo.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
上面的代码就出现问题了,复制的文件打开乱码
目前我还不能解释原因,希望有人能解答下!