////代码//// public class ColligationIO { public static void main(String args[]) throws IOException { OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream( "d:\\test\\test1.txt")); InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String read = br.readLine(); osw.write(read); while (read != null) { if (read.trim().length() > 0) { if (read.equalsIgnoreCase("exit")) return; read = br.readLine(); osw.write(read);
osw.close(); } else { return; } } } } ///////异常/////// Exception in thread "main" java.io.IOException: Stream closed at sun.nio.cs.StreamEncoder.ensureOpen(Unknown Source) at sun.nio.cs.StreamEncoder.write(Unknown Source) at sun.nio.cs.StreamEncoder.write(Unknown Source) at java.io.OutputStreamWriter.write(Unknown Source) at java.io.Writer.write(Unknown Source) at ColligationTest.ColligationIO.main(ColligationIO.java:19)
------解决方案-------------------- while (read != null) { //如果不为空进循环 if (read.trim().length() > 0) { if (read.equalsIgnoreCase("exit")) return; read = br.readLine(); osw.write(read);