我正在尝试写入和反对文件,然后从文件中读取对象并对其执行一些操作。该对象被很好地写入文件,但是当我尝试从文件中检索它时,我什么也没得到。我使用的代码如下:
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();
}
}