Ayer Alobaid
Ayer Alobaid

Reputation: 133

Reading objects from a file and adding them to an array

I'm trying to read objects from a file then add them to an Array List of Ticket. But it's not working. May I please know where's the problem?

public void writeTicketToFile(Ticket ticket) {
    try {
        FileOutputStream fileOut = new FileOutputStream("tickets.txt");
        ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
        objectOut.writeObject(ticket.toString());
        objectOut.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

}

public void readTicketFromFile(){
    ArrayList<Ticket> tickets = new ArrayList<Ticket>();
    try {
        FileInputStream fi = new FileInputStream(new File("tickets.txt"));
        ObjectInputStream oi = new ObjectInputStream(fi);
        Ticket ticket;
        while (ticket=oi.readObject() != null){
            tickets.add((Ticket)oi.readObject());
        }
        System.out.println(tickets);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Upvotes: 0

Views: 269

Answers (3)

MS90
MS90

Reputation: 1249

There are a lot of issues here:

Make sure your Ticket implements Serializable interface for writing/reading objects from/to file as in this simple example:

public class Ticket implements Serializable{

private String name;
private LocalDateTime issued;

public Ticket() {
}

public Ticket(String name, LocalDateTime issued) {
    this.name = name;
    this.issued = issued;
}

/**
 * @return the name
 */
public String getName() {
    return name;
}

/**
 * @param name the name to set
 */
public void setName(String name) {
    this.name = name;
}

/**
 * @return the issued
 */
public LocalDateTime getIssued() {
    return issued;
}

/**
 * @param issued the issued to set
 */
public void setIssued(LocalDateTime issued) {
    this.issued = issued;
}

}


Now pay attention to while writing tickets to a file to write them one at a time. You can achieve it by iterating thru your list of tickets and writing it one at a time, something like:

for (int i = 0; i < tickets.size(); i++) {
                    objectOut.writeObject(tickets.get(i));
                }

Also, make sure to close your ObjectInputStream after reading as it will surely throw EOFException at the end, take a look at implementation of it in readTicketFromFile method.


public class SerializationAndDeserializationOfTicket {

    public static void main(String[] args) {
        List<Ticket> listOfTickets = new ArrayList<>();
        listOfTickets.add(new Ticket("Concert 1", LocalDateTime.now()));
        listOfTickets.add(new Ticket("Concert 2", LocalDateTime.now()));
        listOfTickets.add(new Ticket("Concert 3", LocalDateTime.now()));

        writeTicketToFile(listOfTickets);

        readTicketFromFile();
    }

    public static void writeTicketToFile(List<Ticket> tickets) {

        try {
            FileOutputStream fileOut = new FileOutputStream("tickets.txt");
            ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
            for (int i = 0; i < tickets.size(); i++) {
                objectOut.writeObject(tickets.get(i));
            }
            objectOut.close();
        } catch (IOException e) {
            System.err.println("JVM reported an IO exception. Please, take a look.");
        }
    }

    public static void readTicketFromFile() {
        ArrayList<Ticket> tickets = new ArrayList<>();
        try {
            FileInputStream fi = new FileInputStream(new File("tickets.txt"));
            ObjectInputStream oi = new ObjectInputStream(fi);
            while (true) {
                try {

                    Ticket ticket = (Ticket) oi.readObject();
                    tickets.add(ticket);
                    System.out.println(ticket.getName() + " " + ticket.getIssued());
                } catch (EOFException ex) {
                    oi.close();
                    break;
                }
            }

        } catch (IOException | ClassNotFoundException e) {
            System.err.println("JVM reported an IO/ClassNotFound exception. Please, take a look.");
    }
    }

Upvotes: 0

nullPointer
nullPointer

Reputation: 4574

Just add the Ticket objects into an ArrayList and write the list (instead of each object one by one) as a single object. Then read the list from the file in your readTicketFromFile() method as :

 ArrayList<Ticket> ticketsList = (ArrayList<Ticket>)oi.readObject();

Upvotes: 0

nvioli
nvioli

Reputation: 4209

One of your main problems lies here:

while (ticket=oi.readObject() != null){
  tickets.add((Ticket)oi.readObject());
}

Compare the fact that you're trying to read a Ticket object out of a file with the way you're writing the Ticket to the file:

objectOut.writeObject(ticket.toString());

As you can see, you're converting the Ticket to a String and writing the String to the file. Then when you try to read, you're trying to read a Ticket. Instead, you should read a String, and then convert the String into a Ticket in code.

If Ticket is serializable, you may instead just be able to remove .toString() from the write step, but I've never worked with object streams, so I can't say 100% if that will work.

Upvotes: 1

Related Questions