Pass data using the internal storage in an android application
<p>I tried to implement <a href="http://developer.android.com/guide/topics/data/data-storage.html#filesInternal" rel="nofollow">this example</a> to write and read data from internal storage, but when I'm trying to read written data from the same Android application it does not give me any result. What can be the error?? Following is the code I have used. </p>

<pre><code>public class SecondScreen extends Activity {

  String FILENAME = "hello_file";
  String string = "hello world!";



  /** Called when the activity is first created. */
@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.secondscreen);

  //Using the Internal Storage
    try{
    FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
    fos.write(string.getBytes());
    fos.close();

    }

    catch(Exception e){

    }


     TextView result;
     result = (TextView)findViewById(R.id.status);

 try{

     String test = "";
     FileInputStream fis = openFileInput("hello_file");
     fis.read(test.getBytes());
     fis.close();
     result.setText(test);

    }

    catch(Exception e){

    }


}
</code></pre>

<p>manifest file :</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wrd.ws"
android:versionCode="1"
android:versionName="1.0" &gt;

&lt;uses-sdk android:minSdkVersion="8" /&gt;

&lt;application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" &gt;
    &lt;activity
        android:name=".ReadWriteDataActivity"
        android:label="@string/app_name" &gt;
        &lt;intent-filter&gt;
            &lt;action android:name="android.intent.action.MAIN" /&gt;

            &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
        &lt;/intent-filter&gt;
    &lt;/activity&gt;
&lt;/application&gt;
&lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
&lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;

&lt;/manifest&gt;
</code></pre>

<p>The updated code :</p>

<pre><code>public class ReadWriteDataActivity extends Activity {
    TextView tv;
String line;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    writeFileToInternalStorage();
    readFileFromInternalStorage();
    tv = (TextView) findViewById(R.id.textView);
</code></pre>

<p>//        tv.setText(line);</p>

<pre><code>}
private void writeFileToInternalStorage() 
{
    String eol = System.getProperty("line.separator");
    BufferedWriter writer = null;
    try 
    {

         File myFile = new File("/sdcard/mysdfile.txt");
         myFile.createNewFile();



      writer = new BufferedWriter(new OutputStreamWriter(openFileOutput("myFile.txt", MODE_WORLD_WRITEABLE)));
      writer.write("This is a test1."+ eol);
      writer.write("This is a test2." + eol);
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    } 
    finally 
    {
      if (writer != null) 
      {
        try 
        {
            writer.close();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
      }
    }
}



private void readFileFromInternalStorage() 
{
    String eol = System.getProperty("line.separator");
    BufferedReader input = null;
    try 
    {
        input = new BufferedReader(new InputStreamReader(openFileInput("myFile.txt")));
        //String line;
        StringBuffer buffer = new StringBuffer();
        while ((line = input.readLine()) != null) 
        {
            buffer.append(line + eol);
        }
         tv.setText(buffer.toString().trim());
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    } 
    finally 
    {
        if (input != null) 
        {
            try 
            {
                input.close();
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }
        }
    }
}
</code></pre>

<p>Thanks!</p>
