0

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).

Figure 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).

Figure 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.

1
  • Pavel explained it great! Use his approach if you would like to have text-like stored values. @Kyle Posey Commented Apr 3, 2019 at 21:46

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you! Couple questions for you. Doing it like this can I still read from the file after the program has been been closed and refference each line as an ArrayList object? In more final iterations, the program wont have the object creations, thatll be done in the program by the user. And would I go about adding the permanent header using a formatter?
Yes, you can read it with BufferedReader: BufferedReader reader = new BufferedReader(new FileReader("reservations.txt")); Each line represents a new Passenger object. Read it with reader.readLine(), split the String by a delimiter you use (TAB or space character) and create a new Passenger object.
BTW, I do not know what your requirements are, but, if possible, you should consider using some DBMS for storing and retrieving data, instead of text files. Text files for this purpose is not the best approach.
About the header. I do not think you need a formatter here. Just write it the same way. If you know exactly that the first line is always a header, skip it when reading and write it if you create a new file.
1. Thanks! That works. 2. I'm required to use a text file, not sure why 3. I'm not too sure how I would implement the header and skip the first line when I put it in. Ill have to research it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.