0

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();
    }
}
2
  • 1
    How it is not working? Bare in mind that Ticket needs to implement Serializable interface for writing tickets to some file. Commented Apr 11, 2019 at 13:21
  • 1
    Do not writeObject(ticket.toString()) as this converts your ticket to a string, but this string cannot be parsed back. Commented Apr 11, 2019 at 13:21

3 Answers 3

1

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.

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

2 Comments

I removed the toString() and the object was read, but now, it is only reading one object not all of them that are in the file (or most probably only writing one object)
Glad to hear that worked. This sounds like a different issue, which should probably be a separate question. The code you've provided just shows methods for writing and reading, but not the implementation where the writing is happening. If you think you should be writing more than one Ticket, you'll have to show where that's happening.
0

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.");
    }
    }

1 Comment

THANK YOU IT WORKED
0

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();

Comments

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.