Java 怎样将多个对象读取出来?怎样判断是否读完?
Java 将多个对象读取出来按以下方法进行:
public class SerializableTest1
{
/**
* 保存为一个二进制的文件
*/
public static void main(String[] args) throws Exception
{
Person p1 = new Person(20,"张三",33.4);
Person p2 = new Person(50,"李四",53.4);
Person p3 = new Person(10,"王五",123.4);
//存入序列化的对象
FileOutputStream fos = new FileOutputStream("person.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(p1);
oos.writeObject(p2);
oos.writeObject(p3);
oos.close();
///////////////////////////////////////////////////////
//取出序列化的对象
FileInputStream fn = new FileInputStream("person.txt");
ObjectInputStream ois = new ObjectInputStream(fn);
while(fn.available() > 0) //代表文件中还有内容
{
Person p = (Person)ois.readObject();
System.out.println(p);
2024-07-18 广告