Why I cant get objects from file properly?
up vote
0
down vote
favorite
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?
java serialization
add a comment |
up vote
0
down vote
favorite
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?
java serialization
so before I write to file I need to read it and then write again with new and old objects?
– Michael
Nov 8 at 14:31
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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?
java serialization
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?
java serialization
java serialization
asked Nov 8 at 14:18
Michael
3,8762980147
3,8762980147
so before I write to file I need to read it and then write again with new and old objects?
– Michael
Nov 8 at 14:31
add a comment |
so before I write to file I need to read it and then write again with new and old objects?
– Michael
Nov 8 at 14:31
so before I write to file I need to read it and then write again with new and old objects?
– Michael
Nov 8 at 14:31
so before I write to file I need to read it and then write again with new and old objects?
– Michael
Nov 8 at 14:31
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
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)
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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)
add a comment |
up vote
0
down vote
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)
add a comment |
up vote
0
down vote
up vote
0
down vote
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)
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)
edited Nov 8 at 14:30
answered Nov 8 at 14:24
Antoniossss
14.6k12149
14.6k12149
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53209621%2fwhy-i-cant-get-objects-from-file-properly%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
so before I write to file I need to read it and then write again with new and old objects?
– Michael
Nov 8 at 14:31