androniennn
androniennn

Reputation: 3137

Write a List in a file with a return to the line

I'm facing a problem: I want to store a list then read/display it from a file ! In fact, the problem is when writing the list, it just writes it without any end of line or any conditions, code:

try {
            fOut = context.openFileOutput(nom, Context.MODE_APPEND);
            osw = new OutputStreamWriter(fOut);
            for (int i = 0; i < list.size(); i ++ )
            {
                osw.write(list.get(i));
            }
            osw.flush();
            osw.close();
            fOut.close();

Data stored:

data = date + " : " + ans + " L/100km\n" + litre + " litres "+ km + " km\n";

ListView L = (ListView) findViewById(R.id.lv);

        ArrayAdapter adapter = new ArrayAdapter(this, R.layout.list_item, list);
        L.setAdapter(adapter);

Can i modify this line osw.write(list.get(i));or add another line(s) to make an end of line after every data! Thank you.

Upvotes: 0

Views: 493

Answers (1)

MByD
MByD

Reputation: 137282

osw.write(list.get(i) + "\n"); should do the job.

To add line break every X lines, you can modify your for loop this way:

 for (int i = 0; i < list.size(); i ++ )
     {
         osw.write(list.get(i));
         if ((i+1) % X == 0)  // change X to desired number
            {
               osw.write("\n");
            }
     }

Upvotes: 1

Related Questions