运行bufferedWriter时抛出java.io.IOException: Stream closed
packagetest;importjava.io.*;publicclassTestBufferedReaderAndWriter{/**完成从一个文件中读出所有行写入...
package test;
import java.io.*;
public class TestBufferedReaderAndWriter {
/**完成从一个文件中读出所有行写入另一个文件
* @param args
*/
public static void main(String[] args) {
String s= null;
int i=0;
FileReader fr = null;
FileWriter fw = null;
BufferedReader br = null;
BufferedWriter bw = null;
try{
fr = new FileReader("e:\\123\\1.txt");
fw = new FileWriter("e:\\123\\2.txt");
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
while ((s=br.readLine())!=null){
bw.write(s);
bw.newLine();
}
}catch(IOException e){
e.printStackTrace();
}finally{
try{
fr.close();
fw.close();
br.close();
bw.close();//34行
}catch(IOException e){
e.printStackTrace();
}
}
}
}
运行时抛出java.io.IOException: Stream closed
at sun.nio.cs.StreamEncoder.ensureOpen(Unknown Source)
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.BufferedWriter.flushBuffer(Unknown Source)
at java.io.BufferedWriter.close(Unknown Source)
at test.TestBufferedReaderAndWriter.main(TestBufferedReaderAndWriter.java:34)
为什么bw关闭会抛异常 展开
import java.io.*;
public class TestBufferedReaderAndWriter {
/**完成从一个文件中读出所有行写入另一个文件
* @param args
*/
public static void main(String[] args) {
String s= null;
int i=0;
FileReader fr = null;
FileWriter fw = null;
BufferedReader br = null;
BufferedWriter bw = null;
try{
fr = new FileReader("e:\\123\\1.txt");
fw = new FileWriter("e:\\123\\2.txt");
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
while ((s=br.readLine())!=null){
bw.write(s);
bw.newLine();
}
}catch(IOException e){
e.printStackTrace();
}finally{
try{
fr.close();
fw.close();
br.close();
bw.close();//34行
}catch(IOException e){
e.printStackTrace();
}
}
}
}
运行时抛出java.io.IOException: Stream closed
at sun.nio.cs.StreamEncoder.ensureOpen(Unknown Source)
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.BufferedWriter.flushBuffer(Unknown Source)
at java.io.BufferedWriter.close(Unknown Source)
at test.TestBufferedReaderAndWriter.main(TestBufferedReaderAndWriter.java:34)
为什么bw关闭会抛异常 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询