日期:2014-05-20  浏览次数:20741 次

关于FileIO的基础问题,请指教。
我是一个JAVA初学者,在学到IO这章时碰到个疑惑,请大家指教。

我想实现一个从控制台输入字符串,把字符串的内容写到文本文件里,功能基本实现,但每次输入文字(或字符串)按2次ENTER键的时候,程序就会报错。我想不停的从控制台输入,把输入的内容写到文本文件里,请问怎么实现,谢谢。代码和异常如下:

////代码////
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); 

osw.close(); //这个关闭流在循环里面..
} else { 
return; 





如果你按回车也就是为空吧!它不会进循环..

不进循环就不会关闭流..就报Exception in thread "main" java.io.IOException: Stream closed 

把osw.close();写到循环外面试试看..我也是新手,只是这样认为的..呵呵.
------解决方案--------------------
引用楼主 standsky 的帖子:
我是一个JAVA初学者,在学到IO这章时碰到个疑惑,请大家指教。

我想实现一个从控制台输入字符串,把字符串的内容写到文本文件里,功能基本实现,但每次输入文字(或字符串)按2次ENTER键的时候,程序就会报错。我想不停的从控制台输入,把输入的内容写到文本文件里,请问怎么实现,谢谢。代码和异常如下:

////代码////
public class ColligationIO {
public static void main(String args[]) throws IOException { …

------解决方案--------------------
要注意 flush 问题
Java code

import java.io.*;
////代码//// 
public class ColligationIO { 
  public static void main(String args[]) throws Exception { 
   OutputStreamWriter osw = new OutputStreamWriter(
           new FileOutputStream( 
   "d:\\test\\test1.txt")); 
   InputStreamReader isr = new InputStreamReader(System.in); 
   BufferedReader br = new BufferedReader(isr); 
   String read = null; 
 
   while ( (read=br.readLine()) != null) { 
   if (read.trim().length() > 0) { 
   if (read.equalsIgnoreCase("exit"))
    {
     osw.close(); 
     return; 
    }
   osw.write(read);
   osw.flush();
}//if 
}//while 
}//main 
} //class

------解决方案--------------------
Java code

package csdn;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;


public class ColligationIO { 
    public static void main(String args[]) throws IOException { 
        OutputStreamWriter osw =null;
        BufferedReader br = null;
        try {
            osw = new OutputStreamWriter(new FileOutputStream( 
            "e:\\test\\test1.txt")); 
            InputStreamReader isr = new InputStreamReader(System.in); 
            br = new BufferedReader(isr); 
            String read =null;
            while (true) { 
                read = br.readLine(); 
                if (read.trim().length() > 0) { 
                    if (read.equalsIgnoreCase("exit")) 
                        break; 
                    osw.write(read+"\r\n");     
                    osw.flush(); 
                }
            } 
        }finally {
            if(br!=null)
            br.close();
            if(osw!=null)
            osw.close();
        }
    } 
}