您的位置:首页 > 其它

复制文件有一种方法出现乱码

2013-07-03 08:34 274 查看
今天随手做了复制文件的代码,发现了一个问题
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();
}

}

上面的代码就出现问题了,复制的文件打开乱码

目前我还不能解释原因,希望有人能解答下!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  复制文件 乱码