BufferReader异常
package IO;
import java.io.*;
public class TestIO {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("G:\\shi's work\\java\\question.txt");
fos = new FileOutputStream("G:\\shi's work\\java\\copy1.txt");
;
} catch (
FileNotFoundException e) {
System.out.println("文本地址错误");
e.printStackTrace();
}
BufferedReader br = null;
BufferedWriter bw = null;
String tem;
try{
br = new BufferedReader(new InputStreamReader (fis));
bw = new BufferedWriter(new OutputStreamWriter(fos));
while((tem =br.readLine())!=null){
bw.write(tem);
}
}catch(Exception e){
e.printStackTrace();
}
finally{
try {
br.close();
bw.flush();
bw.close();
} catch (
IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
文件也正确的复制了,但是最后抛出异常,求解释
PS:我删除了与它无关的源代码,所以最后提示的行数不是54
------解决方案--------------------原来的54行是哪句啊?如果是bw.flush();,那你把它移动到br.close();前边
------解决方案--------------------话说我这里没报错
------解决方案--------------------已试,没有异常,但有点问题
bw.write(tem);改成bw.write(tem+"\r\n");最好
关闭的时候先关bw,在关br,你还有fis和fos没有关闭
------解决方案--------------------fis和fos关闭就可以了。