本帖最后由 小小企鹅 于 2012-10-27 22:36 编辑
while((len=fr.read())!=-1) 只读了一个字符,也没有将内容赋值给buf,可以改为 while((len=fr.read(buf))!=-1)
public static void main(String[] args)
{
Copy copyfile= new Copy();
}
Copy(){
try {
copyFromTo("abc.txt","abc_copy.txt");
}catch(IOException e){
throw new RuntimeException("读写失败!");
}
}
public void copyFromTo(String from, String to) throws IOException {
FileReader fr = new FileReader(from);
FileWriter fw = new FileWriter(to);
char[] buf = new char[1024];
int num = 0;
while((num = fr.read(buf)) != -1)
{
String str = new String(buf,0,num);
fw.write(str);
}
fr.close();
fw.close();
} |