1

I'm newbie at developing android application, so I am making this simple application to save a list of data that user input and able to read it as a list later on. I am able to write user input into a file, but every i input another list, it overwrite the first one, what I want is to add this in the file, and call the list as array to be displayed as a list. How do I write an array or achieve this ? Here's the code :

package com.example.storage;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    private EditText et;
    private EditText st;
    private String data;
    private String file;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et = (EditText)(findViewById(R.id.editText1));
        st = (EditText)(findViewById(R.id.showText1));
    }

    public void save(View view) {
    data = et.getText().toString();
        file = st.getText().toString();
        try {
            FileOutputStream fOut = openFileOutput(file,MODE_WORLD_READABLE);
            fOut.write(data.getBytes());
            fOut.close();
            Toast.makeText(getBaseContext(), "file saved",
                    Toast.LENGTH_SHORT).show();
        } catch  (Exception e){
            //TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void read(View view) {
        file = st.getText().toString();
        try {
            FileInputStream fin = openFileInput(file);
            int c;
            String temp = "";
            while( (c = fin.read()) != -1) {
                temp = temp + Character.toString((char)c);
            }
            et.setText(temp);
            Toast.makeText(getBaseContext(),"file read",
                    Toast.LENGTH_SHORT).show();
        } catch(Exception e) {

        }
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Any help greatly appreciated. Thank you in advance.

Charas
  • 1,753
  • 4
  • 21
  • 53

1 Answers1

0

what about using FileWriter(File file, boolean append) in "append" mode? Have a look here: How to append to a text file in android?

Try it like that:

File f = new File(pathToUrFile);
FileWriter fw = new FileWriter(file, true);
fw.append();

If you are going to save more (and different) values give gson a try to save and load vars or even classes from text files.

Best regards!

Community
  • 1
  • 1
H3ADLESS
  • 83
  • 1
  • 10
  • Thx! will be researching on it and try it. I am thinking it might be easier to store an array inside the file though, is it possible ? or do I have to insert string then convert it into an array when calling it ? – Charas May 17 '15 at 09:27
  • Sure. This is possible. Look here: http://stackoverflow.com/questions/17970128/how-to-convert-string-array-to-object-using-gson-json But if you only need to save some Strings it could be easier to simply save them to the file and separate them by a specific char. Then you don't have to use an external library and can easily split them by String.split('char'). – H3ADLESS May 17 '15 at 09:55