[英]reading an object from a .txt in java
我正在嘗試向文件寫入對象,然后從文件中讀取對象並對其執行一些操作。 該對象可以很好地寫入文件,但是當我嘗試從文件中檢索它時,我什么也沒得到。 我使用的代碼如下:
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();
}
}
我認為您必須通過從API文檔ObjectInputStream實現java.io.Serializable來序列化Person
對象
ObjectInputStream is used to recover those objects previously serialized.
下面的代碼(主要功能與您的相同:
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());
}
人類
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;
}
}
輸出:
Person:B1234:Roshane:Nolan:male:Spanish Town:B2134:B3214:150.0:5.11:Sun Apr 14 23:11:27 CDT 2013
您應該指定“無”的意思。 你得到:
您可以將writeObject和readObject添加到您的人員類。 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.