为什么这段java程序运行第一次没异常,运行第二次就出现出错:java.io.StreamCorruptedException: invalid
代码如下importjava.io.*;publicclassTestFIle{publicstaticvoidmain(String[]args)throwsIOExc...
代码如下
import java.io.*;
public class TestFIle {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectOutputStream output = new ObjectOutputStream((new FileOutputStream("className.txt", true)));
ObjectInputStream input = new ObjectInputStream((new FileInputStream("className.txt")));
String a = "123";
String b = "456";
String c = "qwe";
output.writeObject(a);
output.writeObject(b);
output.writeObject(c);
output.close();
try {
while (true) {
String t = (String) input.readObject();
System.out.println(t);
}
} catch (EOFException ex) {
input.close();
System.out.println("末尾");
}
}
} 展开
import java.io.*;
public class TestFIle {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectOutputStream output = new ObjectOutputStream((new FileOutputStream("className.txt", true)));
ObjectInputStream input = new ObjectInputStream((new FileInputStream("className.txt")));
String a = "123";
String b = "456";
String c = "qwe";
output.writeObject(a);
output.writeObject(b);
output.writeObject(c);
output.close();
try {
while (true) {
String t = (String) input.readObject();
System.out.println(t);
}
} catch (EOFException ex) {
input.close();
System.out.println("末尾");
}
}
} 展开
展开全部
while (true) {
String t = (String) input.readObject();
System.out.println(t);
}
你在这写了死循环!
String t = (String) input.readObject();
这句读不进来,当然会报错!
String t = (String) input.readObject();
System.out.println(t);
}
你在这写了死循环!
String t = (String) input.readObject();
这句读不进来,当然会报错!
追问
读到文件末尾的异常会被EOFException捕捉,我试过不是这个原因,谢谢你的回答
追答
另外你的className.txt的路径不全!!!,盘符都没有,真不知道你这个文件是放在哪的?
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询