简体   繁体   English

从Java中的.txt读取对象

[英]reading an object from a .txt in java

I'm trying to write and object to a file then read the object from the file and perform some operation on it. 我正在尝试向文件写入对象,然后从文件中读取对象并对其执行一些操作。 The object gets written to the file fine but when I try to retrieve it from the file I get nothing. 该对象可以很好地写入文件,但是当我尝试从文件中检索它时,我什么也没得到。 The code I used is below: 我使用的代码如下:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Date;


public class Driver 
{
    public static void main(String[] args) throws IOException, ClassNotFoundException 
    {
        FileOutputStream fos = new FileOutputStream("C:\\Users\\Russian\\Desktop\\file.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        Person person = new Person("B1234","Roshane","Nolan","male","Spanish Town",new Date(),
                "B2134","B3214",150.0,5.11);

        oos.writeObject(person);

        oos.flush();
        oos.close();

        //READING FROM THE FILE

        FileInputStream fis = new FileInputStream("C:\\Users\\Russian\\Desktop\\file.txt");
        ObjectInputStream ois = new ObjectInputStream(fis);


        Person object = (Person) ois.readObject();
        System.out.println(object);

        ois.close();
    }

}

I think you have to Serialize the Person object by implementing java.io.Serializable From the API doc, ObjectInputStream 我认为您必须通过从API文档ObjectInputStream实现java.io.Serializable来序列化Person对象

  ObjectInputStream is used to recover those objects previously serialized. 

Code Below(Main function is same as yours: 下面的代码(主要功能与您的相同:

  public static void main(String[] args) throws IOException, ClassNotFoundException 
    {
        FileOutputStream fos = new FileOutputStream("src/main/resources/Test.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        Person person = new Person("B1234","Roshane","Nolan","male","Spanish Town",new Date(),
                "B2134","B3214",150.0,5.11);

        oos.writeObject(person);

        oos.flush();
        oos.close();

        //READING FROM THE FILE

        FileInputStream fis = new FileInputStream("src/main/resources/Test.txt");
        ObjectInputStream ois = new ObjectInputStream(fis);


        Person object = (Person) ois.readObject();
        System.out.println(object.toString());


    }

Person Class 人类

  import java.util.Date;

  public class Person implements java.io.Serializable{

String arg1,arg2,arg3,arg4,arg5,arg6,arg7;
Double d1,d2;
Date date1;
public Person(String string, String string2, String string3,
        String string4, String string5, Date date, String string6,
        String string7, double d, double e) {
    this.arg1=string;
    this.arg2=string2;
    this.arg3=string3;
    this.arg4=string4;
    this.arg5=string5;
    this.arg6=string6;
    this.arg7=string7;
    this.d1=d;
    this.d2=e;
    this.date1=date;


    // TODO Auto-generated constructor stub
}

@Override
public String toString()
{
    return "Person:"+this.arg1+":"+this.arg2+":"+this.arg3+":"+this.arg4+":"+this.arg5+":"+this.arg6+":"+this.arg7+":"+this.d1+":"+this.d2+":"+this.date1;
}


 }

Output : 输出:

Person:B1234:Roshane:Nolan:male:Spanish Town:B2134:B3214:150.0:5.11:Sun Apr 14 23:11:27 CDT 2013

You should specify what you mean by 'nothing'. 您应该指定“无”的意思。 Are you getting: 你得到:

  • null (that will never happen) 空(永远不会发生)
  • an exception (eg if person class or a field inside of that class is not implementing Serializable) 异常(例如,如果人员类或该类中的字段未实现Serializable)
  • a string 'somepackage.Person@hashcodenumber' (in which case you need to override toString() in the Person class) 字符串“ somepackage.Person@hashcodenumber”(在这种情况下,您需要重写Person类中的toString())
  • a Person object without any fields set (see the answer below) 没有设置任何字段的Person对象(请参见下面的答案)
  • any other string - check your 'toString' method in the Person class 任何其他字符串-在Person类中检查您的'toString'方法

You can add writeObject and readObject to your person class. 您可以将writeObject和readObject添加到您的人员类。 http://docs.oracle.com/javase/1.4.2/docs/api/java/io/ObjectInputStream.html http://docs.oracle.com/javase/1.4.2/docs/api/java/io/ObjectInputStream.html

private void writeObject(ObjectOutputStream oos) 
{
    // default serialisation 
    oos.defaultWriteObject();

    // write custom fields
    oos.writeUTF(this.name);
}

private void readObject(ObjectInputStream ois) 
           throws ClassNotFoundException, IOException 
    {
    // default serialisation
    ois.defaultReadObject();

    // read custom fields
    this.name = ois.readUTF();
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM