Java 从文件中读取对象
我在从Java文件读取对象时遇到问题Java 从文件中读取对象,java,serialization,java-io,Java,Serialization,Java Io,我在从Java文件读取对象时遇到问题 文件是一个数组列表 这是保存对象的代码: try { FileOutputStream fileOut = new FileOutputStream("les projets.txt", true); ObjectOutputStream out = new ObjectOutputStream(fileOut); for (projet a : file) { out.writeObject(a); }
文件
是一个数组列表
这是保存对象的代码:
try {
FileOutputStream fileOut = new FileOutputStream("les projets.txt", true);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
for (projet a : file) {
out.writeObject(a);
}
out.close();
} catch (Exception e) {
e.printStackTrace();
}
这是从文件中读取对象的代码::
try {
FileInputStream fileIn = new FileInputStream("les projets.txt");
ObjectInputStream in = new ObjectInputStream(fileIn);
while (in.available() > 0){
projet c = (projet) in.readObject();
b.add(c);
}
choisir = new JList(b.toArray());
in.close();
} catch (Exception e) {
e.printStackTrace();
}
写作正常。问题是阅读。。。它不读取任何对象(projet)有什么问题?正如EJP在评论和中提到的。如果计划在单个文件中写入多个对象,则应写入自定义ObjectOutputStream,因为在写入第二个或第n个对象头信息时,文件将损坏。
正如EJP建议的那样,写为ArrayList,因为ArrayList已经是可序列化的,所以您不应该有这个问题。作为
out.writeObject(file)
并将其作为ArrayList b=(ArrayList)in.readObject()读回代码>
由于某种原因,如果你不能把它写成ArrayList。将custome ObjectOutStream创建为
class MyObjectOutputStream extends ObjectOutputStream {
public MyObjectOutputStream(OutputStream os) throws IOException {
super(os);
}
@Override
protected void writeStreamHeader() {}
}
并将您的writeObject更改为
try {
FileOutputStream fileOut= new FileOutputStream("les_projets.txt",true);
MyObjectOutputStream out = new MyObjectOutputStream(fileOut );
for (projet a : file) {
out.writeObject(a);
}
out.close();
}
catch(Exception e)
{e.printStackTrace();
ObjectInputStream in = null;
try {
FileInputStream fileIn = new FileInputStream("C:\\temp\\les_projets1.txt");
in = new ObjectInputStream(fileIn );
while(true) {
try{
projet c = (projet) in.readObject();
b.add(c);
}catch(EOFException ex){
// end of file case
break;
}
}
}catch (Exception ex){
ex.printStackTrace();
}finally{
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
并将readObject更改为
try {
FileOutputStream fileOut= new FileOutputStream("les_projets.txt",true);
MyObjectOutputStream out = new MyObjectOutputStream(fileOut );
for (projet a : file) {
out.writeObject(a);
}
out.close();
}
catch(Exception e)
{e.printStackTrace();
ObjectInputStream in = null;
try {
FileInputStream fileIn = new FileInputStream("C:\\temp\\les_projets1.txt");
in = new ObjectInputStream(fileIn );
while(true) {
try{
projet c = (projet) in.readObject();
b.add(c);
}catch(EOFException ex){
// end of file case
break;
}
}
}catch (Exception ex){
ex.printStackTrace();
}finally{
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
您好,您的代码中的e.printStackTrace()
行是否打印了任何异常消息?如果是这样的话,你能把追踪贴到问题上吗?我已经编辑了我的问题cz,任何对象都被读取了@mico noo打印了任何异常消息。请不要像那样使用available()。这不是流结束时的有效测试。请参阅Javadoc。检测对象流结束的正确方法是捕获EOFEException。序列化文件不是文本,所以不要称它们为“.txt”。为什么不直接序列化和反序列化ArrayList,并避免循环呢?那么您一定会得到一个异常,与上面的陈述相反。注意:您不能附加到对象输出流,至少在没有特殊代码的情况下是这样;必须捕获或声明为抛出,例如printStackTrace();}应该添加读取对象的finally块try catch中的finally{in.close();}。我已经修改了代码。请使用“与资源一起尝试”来处理关闭的流。@Pshemo感谢您提供的信息。我不确定OP想要1.7版本的代码,现在我有了新的错误!java.io.StreamCorruptedException:无效流头:73720013,该行为:in=new ObjectInputStream(fileIn);