简体   繁体   中英

How to read and write Java ArrayList Objects to and from a file that has a specific required structure

I need to store and read ArrayList Objects to a file, which that itself isn't the issue. I need to store it with a specific format and have it have a "header" of sorts while still having each ArrayList Object be usable from the file. Another part to it, is it needs to be readable by opening the text file itself, so no serialization can be used (Unless I'm just severely mistaken on how to use serialization). Example of how the working file should look below (Figure 1).

图1

I will include all my code below just so nothing important isn't show on accident.

Airline.java

public class Airline extends Application {

@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("Airline.fxml"));
    Scene scene = new Scene(root);
    stage.setTitle("Seat Reservation");
    stage.setResizable(false);
    stage.setScene(scene);
    stage.show();
}

public static void main(String[] args) {
    Passenger p1 = new Passenger(0001, "John Smith", "1A", "AA12");
    Passenger p2 = new Passenger(0002, "Annah Smith", "1B", "AA12");
    //creating arraylist
    ArrayList <Passenger> pList = new ArrayList <Passenger>();
    pList.add(p1);
    pList.add(p2);

    try {
        FileOutputStream fos = new FileOutputStream(new 
        File("reservations.txt"));
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(pList);
        oos.close();
        fos.close();
        FileInputStream fis = new FileInputStream(new 
        File("reservations.txt"));
        ObjectInputStream ois = new ObjectInputStream(fis);
        ArrayList list = (ArrayList) ois.readObject();
        System.out.println(list.toString());
        ois.close();
        fis.close();

    } catch (FileNotFoundException e) {
        System.out.println("File not found");
    } catch (IOException e) {
        System.out.println("Error initializing stream");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    launch(args);
    }

}

Passenger.java

public class Passenger implements Serializable {

    private static final long serialVersionUID = 1L;

    private int id;
    private String name;
    private String seat;
    private String flight;

    Passenger() {
    };

    public Passenger (int idP, String nameP,String seatP, String flightP) {

       this.id = idP;
       this.name = nameP;
       this.seat = seatP;
       this.flight = flightP;

    }

    @Override
    public String toString() {

       return "\n" + id + " " + name + "    " + seat + "    " + flight;

    }

}

The code I have currently shows this when opening the text file (Figure 2 below).

图1

If anyone has any suggestions please let me know! I've been stumped for quite a while now.

Also , if this breaks any rules or doesn't have the proper tags, let me know and I'll remove/edit it.

In your example you use serialization (you can read more about it here Introduction to Java Serialization ), which saves an object to a file in binary format. So basically you're saving the whole ArrayList , including its internal fields and values as an array of bytes.

But what you really need is simply writing to a text file.

Here's one of the ways you can do that using java.io.PrintWriter :

PrintWriter p = new PrintWriter("reservations.txt");
p.write("your text goes here");

And yes, you have to prepare the text for writing manually.
In your case the best approach would be overriding toString() method of Passenger class, so you can write to a file as simply as this:

Passenger p1 = new Passenger(0001, "John Smith", "1A", "AA12");
Passenger p2 = new Passenger(0002, "Annah Smith", "1B", "AA12");
p.write(p1.toString());
p.write(p2.toString());

toString() method has to concatenate required fields(ID, Name, SeatNumber, Flight#) and return them as a single String with a TAB character as a delimiter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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