Slim
Slim

Reputation: 1744

Strange issue with writing/reading ArrayList to a file

I have this code in my activity:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_top_jokes_body);

        final GlobalsHolder globals = (GlobalsHolder)getApplication();

        TextView text = (TextView) findViewById(R.id.txtJoke);
        text.setText(globals.getList().get(globals.clickedPosition));

        ArrayList<String> jokeBodyList = new ArrayList<String>();


        jokeBodyList.addAll(readFromFile());
        jokeBodyList.add(text.getText().toString());

        System.out.println("List content(jokeBodyList): " + jokeBodyList.toString());
        writeToFile(jokeBodyList);
        System.out.println("The file content(new)" + readFromFile().toString());

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.top_jokes_body, menu);
        return true;
    }

    /* Write content to a file */
    private void writeToFile(ArrayList<String> list) {

        FileOutputStream fos;
        if(list != null){
        try {
                fos = openFileOutput("jokesBody2.bjk",Context.MODE_PRIVATE);
                ObjectOutputStream out = new ObjectOutputStream(fos);
                out.writeObject(list.toString());
                out.close();
        } catch (FileNotFoundException e) {
                e.printStackTrace();
        } catch (IOException e) {
                e.printStackTrace(); 
        }
        }else{
            try {
                fos = openFileOutput("jokesBody2.bjk",Context.MODE_PRIVATE);
                ObjectOutputStream out = new ObjectOutputStream(fos);
                out.writeObject("");
                out.close();
        } catch (FileNotFoundException e) {
                e.printStackTrace();
        } catch (IOException e) {
                e.printStackTrace(); 
        }
        }
    }

    /* Read file's content */
    private ArrayList<String> readFromFile() {
        File file = new File("jokesBody.bjk");
        String ret = "";
        ArrayList<String> list = new ArrayList<String>();
        try {
            InputStream inputStream = openFileInput(file.toString());

            if (inputStream != null) {
                InputStreamReader inputStreamReader = new InputStreamReader(
                        inputStream);
                BufferedReader bufferedReader = new BufferedReader(
                        inputStreamReader);
                String receiveString = "";
                StringBuilder stringBuilder = new StringBuilder();

                while ((receiveString = bufferedReader.readLine()) != null) {
                    list.add(receiveString);
                }

                inputStream.close();
                ret = stringBuilder.toString();
            }
        } catch (FileNotFoundException e) {
            Log.e("log activity", "File not found: " + e.toString());
        } catch (IOException e) {
            Log.e("log activity", "Can not read file: " + e.toString());
        }

        return list;
    }

The main idea is to pass the file's content to a ArrayList then to add another value to the same ArrayList and then to write the list back to teh file. There are 2 methods I'm using to read and write on the files, the problem is that something is wrong with the writing and reading. What I mean is that these 2 prints

System.out.println("List content(jokeBodyList): " + jokeBodyList.toString());
System.out.println("The file content(new)" + readFromFile().toString());

are returning some strange values. Here is teh output:

02-13 09:10:22.482: I/System.out(2286): List content(jokeBodyList): [����tT[���sr�java.util.ArrayListx����a��I�sizexp���w���, t�����sr�java.util.ArrayListx����a��I�sizexp���w���   t��t��t��t�.joke 13joke 12Joke 8 ,joke 14 ,joke 13 ,Joke 9t� ,Joke 9t� ,Joke 8t�t� ,Ceco's joket�    ,joke 13xt�     ,joke 12x,  ,joke 12], joke 10]
02-13 09:10:22.542: I/System.out(2286): The file content(new)[����tT[���sr�java.util.ArrayListx����a��I�sizexp���w���, t�����sr�java.util.ArrayListx����a��I�sizexp���w���  t��t��t��t�.joke 13joke 12Joke 8 ,joke 14 ,joke 13 ,Joke 9t� ,Joke 9t� ,Joke 8t�t� ,Ceco's joket�    ,joke 13xt�     ,joke 12x,  ,joke 12]]

This happens even when the text file is empty. What I'm missing? I know that it's something really small, but I'm not able to find it.

Upvotes: 1

Views: 125

Answers (1)

injecteer
injecteer

Reputation: 20699

out.writeObject(list.toString()); is totally wrong! Use out.writeObject(list); this is what the ObjectOutputStream is for.

Reading is the other way around:

ObjectInputStream ois = new ObjectInputStream( new FileInputStream( file ) );
yourArrayList = (ArrayList)ois.readObject();
ois.close();

Upvotes: 3

Related Questions