Studyon Minte9.com
Android




Internal Storage



You can save files directly on the device's internal storage. By default, files saved to the internal storage are 
private to your application and other applications cannot access them (nor can the user). When the user 
uninstalls your application, these files are removed.


1) First example:


	%--- MainActivity.java

	package com.example.myapp;
	import java.io.BufferedReader;
	import java.io.FileInputStream;
	import java.io.FileOutputStream;
	import java.io.InputStreamReader;

	import android.app.Activity;
	import android.content.Context;
	import android.os.Bundle;
	import android.widget.Toast;

	public class MainActivity extends Activity {

	    @Override
	    protected void onCreate(Bundle state) {
		super.onCreate(state);
		setContentView(R.layout.activity_main);
		
		String fname = "my_file";
		String output = "";

		//READ (from file)
		try {    		
			FileInputStream fd = openFileInput(fname);
			BufferedReader reader = new BufferedReader(new InputStreamReader(fd));
			if (fd != null) {
				String line = null;
				while ((line = reader.readLine()) != null){
					output += line +"n";
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//SHOW (String)
		Toast.makeText(getApplicationContext(), 
                                  "Read from file: " + output, Toast.LENGTH_SHORT).show();
			// First time: display - Read from file: 
			// Second time: display - Read from file: My string
		
		//WRITE (to file)
		try {
			FileOutputStream fw = openFileOutput(fname, Context.MODE_PRIVATE);
			fw.write("My string".getBytes());
			fw.close();
		} catch (Exception e) {
				e.printStackTrace();
		}
	    }
	    
	}

	
2) Second example (append, throws Exception)


	%--- activity_main.xml

	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	    xmlns:tools="http://schemas.android.com/tools"
	    android:orientation="vertical"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    tools:context=".MainActivity">
	    
	    <Button
		    android:id="@+id/button_write"
		    android:text="Write"
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content"
		    android:onClick="writeFile"
		    />
	    
	    <Button
		    android:id="@+id/button_read"
		    android:text="Read"
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content"
		    android:onClick="readFile"
		    />
		
	</LinearLayout>


	%--- MainActivity.java

	package com.example.myapp;
	import java.io.BufferedReader;
	import java.io.FileInputStream;
	import java.io.FileOutputStream;
	import java.io.InputStreamReader;

	import android.app.Activity;
	import android.content.Context;
	import android.os.Bundle;
	import android.view.View;
	import android.widget.Toast;

	public class MainActivity extends Activity {

	    @Override
	    protected void onCreate(Bundle state) {
		
		super.onCreate(state);
		setContentView(R.layout.activity_main);	
	    }

	    public void writeFile(View view) { // button method (needs View)
		
		try { 
			writeFile("my_file", "My String");
		} catch (Exception e) {
			e.printStackTrace();
		}
	    }
	    
	    public void readFile(View view) {
			
		try {
			String output = readFile("my_file");
			Toast.makeText(getApplicationContext(), 
                                "File readed: n" + output, Toast.LENGTH_LONG).show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	    }
	    
	    
	    public void writeFile (String fname, String s) 
			throws Exception {
		
		/*--- LOOK HERE ---*/

		FileOutputStream fs = openFileOutput(fname, Context.MODE_APPEND);
		fs.write((s+"n").getBytes());
		fs.close();
		
		Toast.makeText(getApplicationContext(), "File writted", Toast.LENGTH_SHORT).show();
	    }
	    
	    
	    
	    public String readFile(String fname) 
			throws Exception {
		
		String output = "";
		
		FileInputStream fs = openFileInput(fname);
		BufferedReader reader = new BufferedReader(new InputStreamReader(fs));
		if (fs != null) {
			String line = null;
			while((line = reader.readLine()) != null) {
				output += line + "n";
			}
		}
		
		return output;
	    }
	    
	}


http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
http://stackoverflow.com/questions/12157125/writing-and-reading-string-to-an-internal-storage-in-android