public static void main(String[] args) {
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader("C:\my.txt");
fw = new FileWriter("D:\you.txt");
char []buf = new char[1024];
int len = 0;
//读一个数组大小,写一个数组大小方法。
while((len = fr.read(buf)) != -1){
fw.write(buf, 0, len);
}
} catch (Exception e) {
System.out.println(e.toString());
} finally {
if (fr != null)
try {
fr.close();
} catch (Exception e2) {
throw new RuntimeException("关闭失败!");
}
if (fw != null)
try {
fw.close();
} catch (IOException e) {
throw new RuntimeException("关闭失败!");
}
}
}
}
其中while((len = fr.read(buf)) != -1){
fw.write(buf, 0, len);
}
这一句不明白,求详细解答
将my.txt读成流写入you.txt
一直读,如果到了末尾,就会返回 -1,就是判断是否读取完毕
就是一直可以输入,如果你输入没有了,他就停止了
每次读n个,读到结束就是得到-1,就是读完了
就是一直循环读取,如果读完了,就是-1,,就结束了
其实就是让数组下标越界,然后就表示还数组已经读完,循环也结束