Search code examples
androidfileinputstreamfileoutputstreamobjectinputstreamobjectoutputstream

ObjectOutputStream not saving in custom class


I made my own custom class, and I can't figure out what's wrong with it. More specifically my question is what differences exist between making a class for android instead of Java. I mean, according to the logCat it doesn't throw any errors or anything. To the contrary, it tells me it that the InputStream reads correctly. For some reason I can't get it to save. At all. It doesn't throw errors or anything. It just doesn't save the parameters given. Any ideas? I think it's because I'm declaring the parameters for the method saveData incorrectly.

package com.eai.thepicker;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.util.Log;


public class DataHandler extends Activity {

    FileOutputStream file_out;
    FileInputStream file_in;
    ObjectOutputStream obj_out;
    ObjectInputStream obj_in;
    ArrayList<String> retrieved_data, data_out;
    private boolean switch_1;
    private String tag;
    private String default_message;
    Context context;
    ArrayList<String> data_given;

    public DataHandler(Context context_given){
        context = context_given;
    }

    @SuppressWarnings("unchecked")
    public ArrayList<String> retrieveData(){
        tag = "DataHandler";
        default_message = "Tested";

        try {
            file_in = context.openFileInput("array_saved");
            obj_in = new ObjectInputStream(file_in);
            retrieved_data = (ArrayList<String>) obj_in.readObject();
            obj_in.close();
            file_in.close();
            switch_1 = true;
            Log.d(tag, "Loaded");
            } catch (FileNotFoundException e) {
            Log.d(tag, "File Not Found Exception.");
            } catch (IOException e) {
                Log.d(tag, "IO Exception.");
            } catch (ClassNotFoundException e) {
                Log.d(tag, "Class Not Found Exception.");
            } 

        if (switch_1 == false);{
        retrieved_data = new ArrayList<String>();
        retrieved_data.add(default_message);
    }

        return retrieved_data;

    }
    public void saveData(ArrayList<String> data_given){
        try {
            file_out = context.openFileOutput("array_saved", 0);
            obj_out = new ObjectOutputStream(file_out);
            obj_out.writeObject(data_given);
            obj_out.close();
            file_out.close();
            Log.d(tag, "Loaded");
        } catch (FileNotFoundException e) {
        Log.d(tag, "File Not Found Exception.");
        } catch (IOException e) {
            Log.d(tag, "IO Exception.");
            } 
    }


}

Solution

  • You're passing in the wrong scope of variable in your saveData() method. You're calling the class variable this.data_given when you should be calling the instance variable data_given. You haven't assigned any value to the class variable data_given (this.data_given). So, the output stream is working. It's just writing what it contains - which is nothing.

    To answer your second question, there is no difference between creating a class in Android and creating a class in Java. Android is written in Java. So, that would be kind of like saying "What's the difference between a stringed instrument and a violin?" The violin is a stringed instrument. It's just a more specialized implementation. It shares all of the qualities of stringed instrument - and then some. Android is kind of like the violin, so-to-speak. It is Java. Just a more specialized version of Java (with some minor exceptions).

    Hope that helps!