我是一个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)
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)
解决方案 »
- java paint repaint update方法的调用问题
- java,for循环鞋子问题?
- final用来修饰变量、方法、类分别代表什么含义?
- 大家看看这个JAVABEAN是不是写的有毛病??
- wincvs出错了:Couldn't chdir to working directory : Invalid argument
- 求助队列数据结构
- 大家学习一门语言需要多少时间?
- 如何获得数据集的记录数
- 请教:在SQL server 的 JDBC 中怎样设置才能使用ResultSet.isBeforeFirst
- 为什末我的程序不能运行?大家帮忙看看!
- 一个简单的问题, java正则表达式, 提取字符
- POI 操作word问题
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();写到循环外面试试看..我也是新手,只是这样认为的..呵呵.
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;
}
}
} 写到一个方法里面..在主函数里面调用那个方法..
////代码////
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);}//if
}//while
}//main
} //class
////代码////
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
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();
}
}
}