在Java中将对象数组存储到文本文件中。

huangapple 未分类评论49阅读模式
英文:

Store array of objects into text file in Java

问题

private Passenger[] queueArray = new Passenger[30];

public class Passenger
{

private String firstName;
private String surname;
private int secondsInQueue;

Passenger(String firstName, String surname)
{
this.firstName = firstName;
this.surname = surname;
secondsInQueue = 0;
}

public String getName()
{
return firstName + " " + surname;
}

public void setName(String firstName, String surname)
{
this.firstName = firstName;
this.surname = surname;
}

public Integer geSeconds()
{
return secondsInQueue;
}

public void setSecondsInQueue(Integer SecondsInQueue)
{
this.secondsInQueue = SecondsInQueue;
}

public void display()
{
System.out.println(firstName + " " + surname + " " + secondsInQueue);
}
}

I need to save the first name and the surname of the passengers to a text file. And then I need to read the file back to the array.

I am literally so stuck... Any help would be appreciated.

Thank You!

英文:

I need help with saving an array to a text file. So I have the following array

private Passenger[] queueArray = new Passenger[30];

and this is the class that I have

public class Passenger 
{

private String firstName;
private String surname;
private int secondsInQueue;

Passenger(String firstName, String surname)
{
    this.firstName = firstName;
    this.surname = surname;
    secondsInQueue = 0;
}

public String getName() 
{
    return firstName + " " + surname;
}

public void setName(String firstName, String surname) 
{
    this.firstName = firstName;
    this.surname = surname;
}

public Integer geSeconds() 
{
    return secondsInQueue;
}

public void setSecondsInQueue(Integer SecondsInQueue) 
{
    this.secondsInQueue = SecondsInQueue;
}

public void display() 
{
    System.out.println(firstName + " " + surname + " " + secondsInQueue);
}


}

I need to save the first name and the surname of the passengers to a text file. And then I need to read the file back to the array.

I am literally so stuck... Any help would be appreciated.

Thank You!

答案1

得分: 0

将类的实现更改如下,以支持Java序列化。没有要实现的方法,它是一个标记接口。

import java.io.*;
public class Passenger implements Serializable {}

然后,您可以将对象写入具有任何扩展名的任何文件中。然后使用Java反序列化可以读取对象数组并进行迭代。请参考这篇文章,其中有一个简单的示例:https://www.javatpoint.com/serialization-in-java。

在反序列化对象时,可以调用获取数组对象的getter方法并进行迭代。

ObjectInputStream in = new ObjectInputStream(new FileInputStream("f.txt"));  
YourClass s = (YourClass) in.readObject(); 
Passenger[] pasngrArray = s.getPassengerArray(); // 调用方法以获取数组
for (Passenger p : pasngrArray){
    // 在此处编写访问两个属性的代码。
}
英文:

Change the class implementation as below, to support java serialization. No methods to implement, it is a marker interface.

import java.io.*;
public class Passenger implements Serializable {}

Then you can write the objects in to any file with any extension. Then using java deserialization you can read the object array and iterate it through. Refer to this article, it has a simple example https://www.javatpoint.com/serialization-in-java.

At the time you de-serialize the object, you can call the getter method for the array object and iterate it through.

 ObjectInputStream in=new ObjectInputStream(new FileInputStream("f.txt"));  
    YourClass s=(YourClass)in.readObject(); 
    Passenger[] pasngrArray = s.getPassengerArray(); // call a method to get the array
    for (Passenger p : pasngrArray){
// your code to access the two properties here. 
}

huangapple
  • 本文由 发表于 2020年4月9日 01:07:05
  • 转载请务必保留本文链接:https://java.coder-hub.com/61106140.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定