简体   繁体   English

Java对象序列化,无法关闭ObjectOutputStream?

[英]Java Object Serialization ,Unable to close ObjectOutputStream?

hi i'm learing Object Serialization and tried this 嗨,我正在学习对象序列化并试过这个

import java.io.*;
class Employee implements Serializable{
        Employee(int temp_id,String temp_name)
        {
        id=temp_id;
        name=temp_name;
        }
int id;
String name;
}

public class SerialTest{

public static void main(String[] args)
{
        Employee e1=new Employee(14,"John");

        try{
        FileOutputStream fileStream=new FileOutputStream("myserial.ser");
        ObjectOutputStream os=new ObjectOutputStream(fileStream);

        os.writeObject(e1);
        }
        catch(IOException ioex)
        {
        ioex.printStackTrace();
        }
        finally{
        os.close();
        }
}//main ends
}//class ends

The program worked before i put in the call to 在我打电话之前,这个程序工作正常

os.close();

Now i doesn't compile , i get the error saying 现在我没有编译,我得到错误说

SerialTest.java:29: cannot find symbol
symbol  : variable os
location: class SerialTest
    os.close();
    ^

It worked before i tried to close the ObjectOutPutStream , the contents of the serialized file are below , 它在我尝试关闭ObjectOutPutStream之前工作,序列化文件的内容如下,

¬í^@^Esr^@^HEmployee^S<89>S§±<9b>éØ^B^@^BI^@^BidL^@^Dnamet^@^RLjava/lang/String;xp^@^@^@^Nt^@^GSainath ~ ¬í^ @ ^ ESR ^ @ ^ HEmployee 2 -S <89>S§±<9B> EO ^ B ^ @ ^ BI ^ @ ^ BidL ^ @ ^ Dnamet ^ @ ^ RLjava /郎/字符串; XP ^ @ ^ @ ^ @ ^ Nt ^ @ ^ GSainath~
i cant seem to understand where i am going wrong , please help! 我似乎无法理解我哪里出错了,请帮忙!

You want to use the purpose-built try-with-resources : 您想要使用专用的try-with-resources

    try (ObjectOutputStream os =
            new ObjectOutputStream(new FileOutputStream("myserial.ser"));) {
        os.writeObject(e1);
    } catch(IOException ioex) {
        ioex.printStackTrace();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java对象序列化,与ObjectOutPutStream一起使用后如何使用相同文件 - Java object serialization, how to use same file after using it with ObjectOutPutStream IKVM.NET对象序列化与ObjectOutputStream的区别 - IKVM.NET object serialization with ObjectOutputStream differences Java:尝试使用objectoutputstream / objectinputstream读取对象-崩溃 - Java: Trying to read object with objectoutputstream/objectinputstream - crashes Java中的ObjectOutputStream - ObjectOutputStream in Java 通过ObjectOutputStream的writeObject方法进行Java ArrayList序列化将不起作用 - Java ArrayList serialization through ObjectOutputStream's writeObject method won't work 为什么不能用Java发送带有ObjectOutputStream的可序列化对象? - Why can't I send Serializable Object with ObjectOutputStream in Java? ObjectOutputStream.write对象的java.io.NotSerializableException - java.io.NotSerializableException by ObjectOutputStream.write Object 在Java中使用ObjectOutputStream()时,对象必须相同吗? - Must the object be the same one when using ObjectOutputStream() in java? Java 使用 ObjectOutputStream 序列化对象工作一次,在双方都尝试过 reset() - Java serializing object with ObjectOutputStream works once , tried reset() on both sides Java和对象序列化 - Java and Object Serialization
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM