Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing internal storage to save list of integers
    primarykey
    data
    text
    <p>i'm trying to save a list of integers in my application by saving each integer in a new line of a file in the internal storage. For retreiving it I read it line by line and put every linevalue, parsed as integer, in my list of integers. I know a database is better for this kinda stuff, but this should work. </p> <p>I am trying for quite a while now, but it never seems to work. I always get a nullpointerexception when trying to read. I logged "line", it gave the value it should have. But</p> <p>saving one id, adding it as a new string:</p> <pre><code>private void saveToFavorites(Integer saveFav) { String favstr = String.valueOf(saveFav); BufferedWriter writer = null; try { writer = new BufferedWriter(new OutputStreamWriter(openFileOutput("favorites", MODE_WORLD_WRITEABLE))); writer.newLine(); writer.append((favstr)); System.out.println(" added to favs :"+ saveFav); } catch (Exception e) { e.printStackTrace(); } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } } } </code></pre> <p>And the reading method:</p> <pre><code> @SuppressWarnings("null") private List&lt;Integer&gt; readFileFromInternalStorage() { List&lt;Integer&gt; favs = null; BufferedReader input = null; try { input = new BufferedReader(new InputStreamReader(openFileInput("favorites"))); String line; while ((line = input.readLine()) != null) { System.out.println("readFileFromInternalStorage line value: "+ line ); favs.add(Integer.parseInt(line)); } } catch (Exception e) { e.printStackTrace(); System.out.println("readFileFromInternalStorage: fail" ); } finally { if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } } return favs; } </code></pre> <p>Which is in an other activity. I thought it would work but it clearly doesnt. When reading back, the logline: System.out.println("readFileFromInternalStorage line value: "+ line ); displays that the value of line equals the LAST added id,and an empty line, and not the others too. So the line by line saving fails. Also when parsing it to an integer it fails, what is weird because it is only a number.</p> <pre><code>08-01 12:29:54.190: I/System.out(1540): readFileFromInternalStorage line value: 08-01 12:29:54.190: I/System.out(1540): readFileFromInternalStorage line value: 301 </code></pre> <p>Anyone knows what i need to change?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload