下面是一个从控制台输入文本,并将文本保存到桌面的程序当运行一次时程序可以停止,在控制上的内容也可以保存到桌面上文件里。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class T4_1 {
public static void main(String[] args) throws IOException {
//for(int i=1;i<=3;i++){
putIn("C:\\Users\\esws\\Desktop/"+9+".txt");
//}
}
public static void putIn(String str) throws IOException{
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(str)));
String line=null;
while((line=bufr.readLine())!=null){
if("over".equals(line)) break;
bufw.write(line.toUpperCase());
bufw.newLine();
bufw.flush();
}
bufw.close();
bufr.close();
}
}
可是当在主函数里,为该函数调用加上for循环是就会报错!这是为什么?怎么改进可以使程序可以再桌面上产生三个文件(文件内容是从控制台上输入的)?
写法是:
//for(int i=1;i<=3;i++){
putIn("C:\\Users\\esws\\Desktop/"+i+".txt");
//}
报错信息是:
Exception in thread "main" java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:145)
at java.io.BufferedInputStream.read(BufferedInputStream.java:308)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at file.T4_1.putIn(T4_1.java:21)
at file.T4_1.main(T4_1.java:12)
|