Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从文件中读取序列化对象?_Java - Fatal编程技术网

Java 从文件中读取序列化对象?

Java 从文件中读取序列化对象?,java,Java,我想从对象文件中写入和读取可序列化类。我可以将该对象写入文件,但似乎无法读回该对象并将其写入控制台 这两个选项都不起作用。我只想在写入对象文件时读入对象 已尝试的代码: public void readBinary1() throws IOException, ClassNotFoundException { ObjectInputStream input = new ObjectInputStream(new FileInputStream

我想从对象文件中写入和读取可序列化类。我可以将该对象写入文件,但似乎无法读回该对象并将其写入控制台

这两个选项都不起作用。我只想在写入对象文件时读入对象

已尝试的代码:

public void readBinary1() throws IOException, ClassNotFoundException {
        ObjectInputStream input = new ObjectInputStream(new FileInputStream
                                      ("G:\\testobject.tmp"));
        input.readObject();

        System.out.println(new myClass());
    }

    public void readBinary1() throws IOException, ClassNotFoundException {
        ObjectInputStream input = new ObjectInputStream(new FileInputStream
                                      ("G:\\testobject.tmp"));

        System.out.println(new myClass(input.readObject()));
    }
类别:

class myClass implements Serializable {
    myClass() {}

    myClass(myClass b) {
       this.a = b.a;
       this.b = b.b;
    }

    private static final long serialVersionUID = 1L;
    private int a = 0;
    private int b = 100;
    private transient int c = 50;
}
新增代码:

    I had to add a toString to my class to do it the way that it was suggested to do. That seems to be the way that is easiest in the short run but I would rather write the object and then be able to read in the object with out having to use the toString. Is there a way that I can read in the object with one read and then be able to break the info apart with the .dot notation. Such as mc.variable1 and mc.variable2 and so on. I had to type cast the read object also before the code would compile.
有两个输入和输出流,允许将对象序列化到文件。我确信有不同的方式来包装阅读类,我想知道哪种方式是创建阅读的最佳方式。

这样做:

myClass readedObject = (myClass) input.readObject();
System.out.println(readedObject);

尝试在堆栈溢出上搜索此问题--实际上有几十个类似的问题和答案。请看,您似乎不明白您正在从流中读取序列化对象<代码>MyClass mc=(MyClass)input.readObject()。我知道我正在读取一个序列化对象。我认为它将整个对象读入变量,然后我必须将这些信息分解成我想用它做的事情,比如写、打印或其他。如果我从一个文件中读取一个对象,它应该一次读取所有对象,就像我从文件中读取一个字符串一样。它将一次读取一个字符串。