title: Android - read internal data
body: <p>I'm trying to make app, and I need to save string, just like TinyDB in AppInventor. So, I found there <a href="http://developer.android.com/guide/topics/data/data-storage.html#filesInternal" rel="nofollow">http://developer.android.com/guide/topics/data/data-storage.html#filesInternal</a> that what  I'm looking for is saving data to internal storage, but I don't know how to read it. They say: </p>
  
  <p>To read a file from internal storage:</p>
  
  <ol>
  <li><p>Call openFileInput() and pass it the name of the file to read. This returns a FileInputStream.</p></li>
  <li><p>Read bytes from the file with read().</p></li>
  <li><p>Then close the stream with close()</p></li>
  </ol>
  
  <p>But I don't know how, my code never works. So I googled how to read from internal storage, and no code worked. Can you please tell me, how to read text from internal storage? :)</p>
  
  <p>This is the code:</p>
  
  <pre><code>    EditText tagbox = (EditText)findViewById(R.id.editText1);
      String tag = tagbox.toString();
      Context context = getApplicationContext();
  
  
      FileInputStream fis = context.openFileInput( tag );
      InputStreamReader in = new InputStreamReader(fis);
      BufferedReader br = new BufferedReader(in);
      String data = br.readLine();
  
          Toast.makeText(getApplicationContext(), data, Toast.LENGTH_LONG).show();
  </code></pre>
  
  <p><strong>Ok, so I found the solution. The easiest way to store data ,like TinyDB in AppInventor, is using SharedPreferences:</strong></p>
  
  <pre><code> SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
  
   Edtior editor = preferences.edit();
  </code></pre>
  
  <p>To store data:</p>
  
  <pre><code>editor.putString("key", "value");
  </code></pre>
  
  <p>To read data:</p>
  
  <pre><code>String value = preferences.getString("key");
  </code></pre>