Java Serialization Object - java

For my android studio project I need to serialize a User object. The idea is to save and load this object, so when the application is closes (gets destroyed) the objects and it's attributes are saved and stored in a file. My problem is that I'm getting an error with loading the object. It's says there is no such file or directory named 'save.bin', although I tried to create it. Here is my code:
public class SaveAndLoadManager{
public void save(Object object){
Log.d("Save", "saving object");
try{
File userInfo = new File("save.bin");
FileOutputStream fileOutputStream = new FileOutputStream(userInfo);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(object);
objectOutputStream.close();
Log.d("Save", "object saved");
}catch(FileNotFoundException e){
Log.e("Save", e.getMessage());
}catch(IOException e){
Log.e("Save", e.getMessage());
}
}
public Object load(){
Log.d("Load", "loading object");
Object object = null;
try{
FileInputStream fileInputStream = new FileInputStream("save.bin");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
object = objectInputStream.readObject();
objectInputStream.close();
Log.d("Load", "object loaded");
}catch(FileNotFoundException e){
Log.e("Load", e.getMessage());
}catch(StreamCorruptedException e){
Log.e("Load", e.getMessage());
}catch(Exception e){
Log.e("Load", e.getMessage());
}
return object;
}
}
How do I properly create a directory?

Assuming that User is implementing Serialazible
File userInfo = new File("save.bin");
you can think about the above line, like a file object, called "save.bin", stored in the root, (/) on the the filesystem. This location is not writable by your application. Your application has the permission two write locations, one stored under /data/data/yourapp, and the other stored on /sdcard/Android/data/yourapp. Both should be access through the public api. E.g. you can use the pair openFileOutput/openFileInput to retrieve the FileOutputStream/FileInputStream
FileOutputStream fileOutputStream = openFileOutput("save.bin", Context.MODE_PRIVATE)
FileInputStream fileInputStream = openFileInput("save.bin");
As mentioned by #androidLerner in his comment,
you can find more info here,

Related

Serialization throws InvalidClassExceptionn

I'm new serialization (and programming in general) so i don't really know what happens under the hood. I'm making an email client program and the class
FileHandlerObject is used to write and read email messages that i send. I maintain an ArrayList and it is read, updated with new email and then written again. I don't understand why it throws an InvalidClassException because the file contains an ArrayList so the casting should be no problem.
I'm maintaining an ArrayList because the writeObject() truncates the file every time it writes.(Let me know if im wrong here)
class FileHandlerObject implements MyFileHandler<EmailMessage>{
public void write(EmailMessage input){
try
{
FileInputStream fileInputStream=new FileInputStream("emails.ser");
ObjectInputStream objectInputStream=new ObjectInputStream(fileInputStream);
#SuppressWarnings("unchecked")
ArrayList<EmailMessage> messagelist= (ArrayList<EmailMessage>) objectInputStream.readObject();
objectInputStream.close();
messagelist.add(input);
FileOutputStream fileOutputStream = new FileOutputStream("emails.ser");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(messagelist);
objectOutputStream.flush();
objectOutputStream.close();
}
catch(FileNotFoundException f)
{
try{
FileOutputStream fileOutputStream = new FileOutputStream("emails.ser");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
ArrayList<EmailMessage> messagelist=new ArrayList<EmailMessage>();
messagelist.add(input);
objectOutputStream.writeObject(messagelist);
objectOutputStream.flush();
objectOutputStream.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
catch(IOException e){
e.printStackTrace();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
}
public ArrayList<EmailMessage> read(){
try{
FileInputStream fileInputStream=new FileInputStream("emails.ser");
ObjectInputStream objectInputStream=new ObjectInputStream(fileInputStream);
#SuppressWarnings("unchecked")
ArrayList<EmailMessage> messagelist= (ArrayList<EmailMessage>) objectInputStream.readObject();
objectInputStream.close();
return messagelist;
}
catch(IOException e)
{
e.printStackTrace();
}
catch(ClassNotFoundException c)
{
c.printStackTrace();
}
catch(ClassCastException c)
{
c.printStackTrace();
}
return null;
}
As the docs explain, this means one of a few things. The code you pasted does not give any insights about which of these mistakes you've made.
You saved some emails. Then, you edited the EmailMessage class, for example by adding a method or changing the name of a field, and then you ran the code again which ends up reading that file that was 'saved' with an old version of EmailMessage and which you are now trying to read with a new version. The java serialization mechanism can theoretically deal with versioning schemes but in practice it's a disaster. Ditch serialization, write your own protocol instead.
The EmailMessage class does not implement Serializable and does not have a no-args constructor. Give it a no-args constructor, and add implements Serializable to it.
Your classpath is incomplete; EmailMessage contains fields or methods that refer to some other class you wrote and it is not available on the classpath right now. Fix your classpath.

Writing an ArrayList of Objects to a file

public class Inventory implements Serializable {
ArrayList<Product> productlist;
File file;
public Inventory(){
productlist = new ArrayList<Product>();
file = new File("build/classes/inventory/inv.ser");
if(!file.exists()){
try {
file.getParentFile().mkdirs();
file.createNewFile();
} catch (IOException ex) {
Logger.getLogger(Inventory.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(file.length() !=0){
loadFile(file);
}
}
public void addProduct(Product product){
productlist.add(product);
saveFile(this.file);
}
public void saveFile(File file){
try{
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(productlist);
out.close();
fos.close();
} catch(FileNotFoundException ex){System.out.println("FileNotFoundException");}
catch(IOException ex){System.out.println("InputException");}
}
public void loadFile(File file){
try{
FileInputStream fis = new FileInputStream(file);
ObjectInputStream in = new ObjectInputStream(fis);
productlist=(ArrayList<Product>)in.readObject();
in.close();
fis.close();
} catch(FileNotFoundException ex){ System.out.println("FileNotFoundException"); }
catch(IOException ex){System.out.println("OutputException");}
catch(ClassNotFoundException ex){System.out.println("ClassNotFoundException");}
}
}
Does writeObject() overwrite the content of the existing file or append the objects to the existing file?
And is it a good idea to serialize an ArrayList of Objects like what i did inside the saveFile method?
Does writeObject() overwrite the content of the existing file or append the objects to the existing file?
Neither. It writes the object to the underlying stream. The underlying stream is a serial byte stream that can only be appended to. In this case the underlying stream is backed by an underlying file, which has or has not already been overwritten, depending on how you constructed the FileOutputStream. It has nothing to do with writeObject(). In any case you can't successfully append to a file of serialized objects without taking special measures.
And is it a good idea to serialize an ArrayList of Objects like what i did inside the saveFile method?
Compared to what?
N.B.
When you get an exception, print it. Not just some message of your own devising.
Creating a file just so you can test it for zero length doesn't make sense.
The directory build/classes/inventory won't be there at runtime once you stop using the IDE. This is no place to put a file.
You could try FileUtils function to write list of object into plane text file.
Please find below URL for reference -
http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#writeLines-java.io.File-java.util.Collection-java.lang.String-boolean-
e.g.
FileUtils.writeLines(new File(fileToAttach), list);

Android saving an ArrayList<CustomObject> onPause and onSaveInstanceState

I have an ArrayList filled with some custom POJO's that I'd like to persist when the user switches screen orientation (onSaveInstanceState) and when e.g. the user hits the back button (onPause).
As far as I know, the SharedPreferences can only hold primitive data types and bundles can not hold references to generic ArrayLists.
Any tips on how to tackle this?
Regards,
Marcus
1- create a class and put everything you want to store for example arraylist of your POJO and make that class implement Serializable interface.
class MyBundle implements Serializable {
ArrayList<POJO> mPOJO;
MyBundle( ArrayList<POJO> pojo){
mPOJO= pojo;
}
}
2- write it to file:
ObjectOutputStream oos = null;
FileOutputStream fout = null;
try{
FileOutputStream fout = new FileOutputStream("Your file path");
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(mb); // mb is an instance of MyBundle
} catch (Exception ex) {
e.printStackTrace();
}finally {
if(oos != null){
oos.close();
}
}
and to get back everything:
ObjectInputStream objectinputstream = null;
try {
streamIn = new FileInputStream("Your file address");
objectinputstream = new ObjectInputStream(streamIn);
MyBundle mb = (MyBundle) objectinputstream.readObject();
} catch (Exception e) {
e.printStackTrace();
}finally {
if(objectinputstream != null){
objectinputstream .close();
}
}
i don't know this is correct method or not but i handle this like this this always success while your app lost all cache data itself then also u can get back serializable object->
for generic ArrayLists always use serializable
just look at once http://developer.android.com/reference/java/io/Serializable.html
Try converting the List to Json using Gson or Jackson.
Store the string in the Shared preference. some thing like below code
String listString = gsonSD.toJson(list<object> instance);
SharedPreferences storeDataPref = getContext().getSharedPreferences("list_store_pref", Context.MODE_PRIVATE);
Editor storeDataEditor = storeDataPref.edit();
storeDataEditor.putString("liststringdata", listString).apply();

Java reading a vector from a txt file gives error

i am trying to write and read a vector to a text file. I made a class WriteVectorToFile (shown below) which makes a vector containing objects of a class called Car. The class Car implements Serializable and only contains information with setter and getter methods. In the WriteVectorToFile class i have made a createVector() method and writetoFile() method and they work. The problem in the readtoFile() method and it gives out an error (listed below). I want to know what am i doing wrong and what is causing the problem.
The error :
java.lang.NullPointerException
at WriteVectorToFile.readtoFile(WriteVectorToFile.java:73)
at WriteVectorToFile.main(WriteVectorToFile.java:110)
at __SHELL1.run(__SHELL1.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at bluej.runtime.ExecServer$3.run(ExecServer.java:725)
Code:
import java.io.*;
import java.util.Vector;
public class WriteVectorToFile{
private Car car1;
private Vector garage;
private File myFile;
private FileOutputStream out;
private FileInputStream in;
private ObjectInputStream objin;
private ObjectOutputStream objout;
public WriteVectorToFile(){
this.myFile = new File("E:/JAVA/My Java Programs/MyVectorFile.txt");
try{
myFile.createNewFile();
System.out.println("New File --> Success.");
}catch(IOException e){
e.printStackTrace();
System.out.println("New File --> Fail.");
}
}
private void writetoFile(){
try{
out = new FileOutputStream(myFile);
objout = new ObjectOutputStream(out);
}catch(FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
try{
objout.writeObject(garage);
objout.close();
System.out.println("Write File --> Success.");
}catch(IOException e){
System.out.println("Write File --> Fail.");
e.printStackTrace();
}
}
private void readtoFile(){
try{
FileInputStream in = new FileInputStream(myFile);
ObjectInputStream objin = new ObjectInputStream(in);
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
// Object obj = null;
Vector tempVec = new Vector();
try{
ERROR Line 73 : tempVec = (Vector) objin.readObject();
objin.close();
System.out.println("Read File --> Success.");
}catch(Exception e){
System.out.println("Read File --> Fail.");
e.printStackTrace();
}
//Car tempg = new Car();
//tempg = (Car) vecNew.firstElement();
//System.out.println(tempg.toString());
//System.out.println(((Car)(vecNew.firstElement())).toString());
}
private void createVector(){
this.garage = new Vector();
// To create a vector with a specific datatype add <type>
// Vector garage = new Vector<Car>();
car1 = new Car("3245","Toyota","Ferry23",(double)34500);
this.garage.add(car1);
this.garage.add(new Car("3232","Fiat","MozoZ3",(double)25000));
this.garage.add(new Car("2345","Mazda","ZenFix",(double)13700));
}
public static void main (String[] args){
WriteVectorToFile test = new WriteVectorToFile();
test.createVector();
test.writetoFile();
test.readtoFile();
}
}
Firstly, apologies as I cannot post it as comment so putting it into an answer block:
Your problem is the scope mismatch involving try-catch blocks . If you have done any C++, the objects are passed by value or reference so this could have been avoided very conveniently. But Java passes everything by value so when you do:
try{
FileInputStream in = new FileInputStream(myFile);
ObjectInputStream objin = new ObjectInputStream(in);
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
The object objin is created inside try-catch scope which is NOT the same when you use it in:
tempVec = (Vector) objin.readObject();
objin.close();
System.out.println("Read File --> Success.");
}catch(Exception e){
System.out.println("Read File --> Fail.");
e.printStackTrace();
}
So the solution is put everything in one "try" block and catch all the exceptions seperately (or even together using Exception as superclass [not a good practice]). This should do the job. Tell us if you manage to solve it anyway.
Because you have defined the private ObjectInputStream objin; as instance variable and it is null. So when you invoke objin.readObject(); on null will throw NullPointerException. Inside the readtoFile() method, you do something like this:
try{
FileInputStream in = new FileInputStream(myFile);
ObjectInputStream objin = new ObjectInputStream(in);
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
That defines a new local ObjectInputStream objin = new ObjectInputStream(in); inside try block, which is invisible to the code tempVec = (Vector) objin.readObject(); ouside try-catch block. Here objin refers to the instance variable declared at instance level and it is null. To correct it change
ObjectInputStream objin = new ObjectInputStream(in);
to
objin = new ObjectInputStream(in);
inside the try block.

ObjectInputStream gives me an empty object (but the readed file is not empty)

I have this serializable class, wich i use to store on a binary file an ArrayList of Strings.
public class SaveState implements Serializable{
public static ArrayList <String> favoriteBusStopNumbers = new ArrayList<String>();
public static SaveState instance=new SaveState();
}
I'm using this method to store the instance with the arrayList of strings once this array is full with the data i must store:
public static void saveData(){
ObjectOutput out;
try {
//primero comprobamos si existe el directorio, y si no, lo creamos.
File folder = new File(Environment.getExternalStorageDirectory() + DIRECTORY_NAME);
if(!folder.exists())
folder.mkdirs();
File outFile = new File(Environment.getExternalStorageDirectory(), DIRECTORY_NAME+"appSaveState.data");
out = new ObjectOutputStream(new FileOutputStream(outFile));
out.writeObject(SaveState.instance);
out.close();
} catch (Exception e) {e.printStackTrace();}
}
And finally, i use this method on the init of my app to load the file and fill my SaveState.instance variable with the previously stored data:
public static void loadData(){
ObjectInput in;
try {
File inFile = new File(Environment.getExternalStorageDirectory(), DIRECTORY_NAME+"appSaveState.data");
in = new ObjectInputStream(new FileInputStream(inFile));
SaveState.instance=(SaveState) in.readObject();
in.close();
} catch (Exception e) {e.printStackTrace();}
}
When i save the data, the file is being created correctly with the object fill of data, i know it because the file has more than 0KB of disc space. But something is going wrong here because when i start my app and load the data, my SaveState.instance variable gets an empty ArrayList of strings....... then ¿what is wrong in the code?
Thanks
Your arraylist is static. Static variables are associated with the class and not with the object. So they dont get serialized.
If you do want to serialize static variables, you have to override readObject and writeObject.
Here is some additional info -- http://java.sun.com/developer/technicalArticles/Programming/serialization/
Best solution here is to reconsider your data structure and make the arraylist non-static if its saving the state of an object.
EDIT: Alternatively, you could serialize just the arraylist ( as #Ted suggests below )
The problem is that variables marked as static will not be serialized except you implement
private void readObject(ObjectInputStream ois){}
and
private void writeObject(ObjectOutputStream oos){}
ObjectOutputStream - JavaDoc:
The default serialization mechanism for an object writes the class of
the object, the class signature, and the values of all non-transient
and non-static fields.
Since the only data kept in SaveState is a static array, you'll have more success just serializing and deserializing the array:
public static void saveData(){
ObjectOutput out;
try {
//primero comprobamos si existe el directorio, y si no, lo creamos.
File folder = new File(Environment.getExternalStorageDirectory() + DIRECTORY_NAME);
if(!folder.exists())
folder.mkdirs();
File outFile = new File(Environment.getExternalStorageDirectory(), DIRECTORY_NAME+"appSaveState.data");
out = new ObjectOutputStream(new FileOutputStream(outFile));
out.writeObject(SaveState.favoriteBusStopNumbers);
out.close();
} catch (Exception e) {e.printStackTrace();}
}
#SuppressWarnings("unchecked")
public static void loadData(){
ObjectInput in;
try {
File inFile = new File(Environment.getExternalStorageDirectory(), DIRECTORY_NAME+"appSaveState.data");
in = new ObjectInputStream(new FileInputStream(inFile));
SaveState.favoriteBusStopNumbers=(ArrayList<String>) in.readObject();
in.close();
} catch (Exception e) {e.printStackTrace();}
}
(You need to suppress the warning about casting to a generic type.)
You don't really need the SaveState.instance field at all.

Categories