我有一个简单的应用程序,但是会丢弃一个文本文件(这只是练习)我只有 3 天使用 Java。问题是在您运行程序之前没有错误,然后它会引发异常并停止。谢谢你。这是一个例外:
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 test.main(test.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at
edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
这就是代码。
import java.io.FileWriter;
import java.util.Random;
import java.io.IOException;
public class test {
public static void main(String[] args) throws IOException {
final String alphabet = "abcdefghigklmnopqrstuvwxyz";
final int N = alphabet.length();
Random r = new Random();
FileWriter file = new FileWriter("hello.txt");
String sb = " ";
for (int i = 0; i < 1;) {
sb += alphabet.charAt(r.nextInt(N));
System.out.println(sb);
int length = sb.length();
file.write(sb);
file.close();
if (length == 30) {
sb = " ";
}
}
}
}