'Data management from file in Android

I am not able to save data from file into an object.If I recover the data from the file and save them in a string, the application works, but as soon as I try to save them inside an object (a Note object in my case), I fail . It is a simple app(similar to notepads) with only a mainActivity to manage operations, an object class that include two variable, one for "title" and another one for "text" and an xml file with two input fields, two buttons(load and save) and a textView to view the data.

This is the the mainActivity code:

   public class MainActivity extends AppCompatActivity {
    
        EditText title,text;
        Button save,load;
        TextView result;
        ArrayList<Note> notes = new ArrayList<>();
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            title = findViewById(R.id.id_title);
            text = findViewById((R.id.id_post));
            save = findViewById(R.id.buttonSave);
            load = findViewById(R.id.buttonLoad);
            result = findViewById(R.id.id_result);
    
            Context context = getApplicationContext();
    
            save.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    String titolo = title.getText().toString();
                    String post = text.getText().toString();
                    //Note nota = new Note(titolo,post);
                    Note nota = new Note(titolo,post);
                    //nota.setTitle(titolo);
                    //nota.setNote(post);
    
                    saveData(nota,context);
    
                }
            });
    
            load.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //ArrayList<Note> data;
                    //String x;
                    //x= readData(context);
                    //x = data.getTitle() + " " +data.getNote();
                    //result.setText(x);
                    //setTextToTextview();
                    readData(context);
                }
            });
        }
    
        /**
         * This function saves data on file
         * @param nota
         * @param context
         */
        public void saveData(Note nota,Context context) {
    
            try {
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("text.txt", Context.MODE_APPEND));
                outputStreamWriter.write(nota.getTitle().toString() + "," + nota.getNote().toString() + "\n");
                outputStreamWriter.flush();
                outputStreamWriter.close();
                Toast.makeText(MainActivity.this, "Saved", Toast.LENGTH_SHORT).show();
            }
            catch (IOException e)
            {
                e.getMessage();
            }
    
        }
    
    
    private void setTextToTextview() {
    
            String text = "";
    
            for(int i = 0; i < notes.size(); i++)
            {
                text = text + notes.get(i).getTitle() + "" + notes.get(i).getNote() + "\n";
            }
    
            result.setText(text);
        }



    /**
     * Read data from file
     */

    public void readData(Context context) {

        String stringa = "";
        String part[];
        //StringBuilder stringBuilder = new StringBuilder();
        try
        {
            InputStream inputStream = context.openFileInput("text.txt");
        if (inputStream != null) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String receiveString = "";


            while ((receiveString = bufferedReader.readLine()) != null) {

                StringTokenizer token = new StringTokenizer(receiveString, ",");
                Note nota = new Note(token.nextToken(), token.nextToken());
                notes.add(nota);
            }
            inputStream.close();
            bufferedReader.close();
            setTextToTextview();
            //stringa = stringBuilder.toString();

            }
        }
        catch (final IOException e)
        {
            e.printStackTrace();
        }

    }


    /*******************************************************
     *this work
     *******************************************************/
    /*
    public String readData(Context context)
    {
        String ret = "";
        try {
            InputStream inputStream = context.openFileInput("text.txt");
            if (inputStream != null) {
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String receiveString = "";
                StringBuilder stringBuilder = new StringBuilder();

                while ((receiveString = bufferedReader.readLine()) != null) {
                    stringBuilder.append("\n").append(receiveString);

                }
                inputStream.close();
                bufferedReader.close();

                ret = stringBuilder.toString();
    }

    } catch (IOException e) {
        e.getMessage();
    }

        return ret;
}
*/


    /*****************************************
     * this work
     *******************************************/
/*
    public String readData(Context context)
    {
       // Note nota = new Note();
        String stringa ="";
        String ret = "";
        try {
            InputStream inputStream = context.openFileInput("text.txt");
            if (inputStream != null) {
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String receiveString = "";
                StringBuilder stringBuilder = new StringBuilder();

                while ((receiveString = bufferedReader.readLine()) != null) {
                   stringBuilder.append("\n").append(receiveString);
                   ret = stringBuilder.toString();

                  // StringTokenizer token = new StringTokenizer(ret,",");




                   String split[]= ret.split(",");

                   for(int i = 0; i< split.length;i++)
                    {
                        stringa = stringa +"," + split[i];

                    }


/*

                   nota.setTitle(split[0]);
                   nota.setNote(split[1]);

                   notes.add(nota);
*/
    /*
                }
                inputStream.close();
                bufferedReader.close();
                //setTextToTextview();

               //ret = stringBuilder.toString();
            }
        } catch (IOException e) {
            e.getMessage();
        }

        return stringa;
    }

*/

    }

I also tried like this:

public void readData(Context context) {

String stringa = "";
String part[];
//StringBuilder stringBuilder = new StringBuilder();
try
{
    InputStream inputStream = context.openFileInput("text.txt");
if (inputStream != null) {
    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    String receiveString = "";
    StringBuilder stringBuilder = new StringBuilder();

    while ((receiveString = bufferedReader.readLine()) != null) {
        stringBuilder.append("\n").append(receiveString); 
        stringa = stringBuilder.toString();
        part = stringa.split(",",2);
        Note nota = new Note(part[0],part[1]);
        notes.add(nota);
        setTextToTextview();

    }
    inputStream.close();
    bufferedReader.close();

    //stringa = stringBuilder.toString();

    }
} catch (final IOException e) {
    e.printStackTrace();
}


//return stringa;

}

thanks to anyone who can help me.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source