简体   繁体   中英

Why I can't get objects from file properly?

I have list of custom objects that I writre and then read to file.

Here is my class:

public class Book implements Serializable{
   private int isbn;
   private String category;
   private String authorName;

    public int getIsbn() {
        return isbn;
    }

    public String getCatId() {
        return category;
    }


    public void setIsbn(int isbn) {
        this.isbn = isbn;
    }

    public void setCategory(String category) {
        this.category = category;
    }


    public void setAuthorName(String authorName) {
        this.authorName = authorName;
    }

    public String getAuthorName() {
        return authorName;
    }

    public Book() {}

    //copy book 
    public Book(Book book, int id) {
        this.category = book.category;
        this.title = book.title;
        this.authorName = book.authorName;
        this.isbn = id;
    } 
}

Here is function that I use to write the list of objects:

private static <T> void writeToFile(List<T> items, String fileName) {
    try {
        String path = "src\\hw1\\library\\repos\\" + fileName;

        FileOutputStream f = new FileOutputStream(path, true);// true- means append to file
        ObjectOutputStream o = new ObjectOutputStream(f);

        // Write objects to file
        o.writeObject(items);
        o.close();
        f.close();
    } catch (Exception e) {}
}

And here is function that I reade from the list:

private static <T> List<T> readFromFile(Context context, String fileName) {
    try {
         String path = "src\\hw1\\library\\repos\\" +fileName ;

         FileInputStream fi = new FileInputStream(path);
         ObjectInputStream oi = new ObjectInputStream(fi);
         // Read objects
         List<T> items = (List<T>)oi.readObject();
         oi.close();
         fi.close();

         return items;
    } catch (Exception e) {}
    return null;
}

The List of objects is written to the file, but when I try to read it with function above the objects that I get from file only objects that was written to file first time.

Any idea why I get from file only objects that was written to file first time?

I only asume that

but when I try to read it with function above the objects that I get from file only objects that was written to file first time.

Single write will put given list into a file, second write will put another list into that file - because you are appending to file

Now, you read method always reads from the begining so every invocation of readFromFile will result in reading the same (very first) object (list in your case) from given file. You would have to do do more f.readObject() on the same stream.

If you are not going to read all objects at once, I would suggest to use 1 file per list, so you will not have use file seeking to "shift" position of file pointer (nor doing empty f.readObject() invocations)

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