Studyon Minte9.com
Android




Resource Storage



If you want to save a static file in your application at compile time, save the file in your project res/raw/ 
directory. You can open it with openRawResource(), passing the R.raw.<filename> resource ID. This method
returns an InputStream that you can use to read the file (but you cannot write to the original file).


	%--- main_activity.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_sync"
		    android:text="Write app file (from raw file)"
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content"
		    android:onClick="writeAppFile"
		    />
	    
	    <Button
		    android:id="@+id/button_read"
		    android:text="Read from App data"
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content"
		    android:onClick="readAppFile"
		    />
	    
	    <Button
		    android:id="@+id/button_reset"
		    android:text="Reset App file"
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content"
		    android:onClick="resetAppFile"
		    />
		
	</LinearLayout>


	%--- MainActivity.java

	package com.example.myapp;
	import java.io.BufferedReader;
	import java.io.FileInputStream;
	import java.io.FileOutputStream;
	import java.io.InputStream;
	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 writeAppFile (View view) throws Exception {

			String content = "";
			
			/*--- LOOK HERE ----*/

			//READ (raw file) - res/raw/my_raw_file
			InputStream fr = getResources().openRawResource(R.raw.my_raw_file);
			BufferedReader reader = new BufferedReader(new InputStreamReader(fr));
			String line = null;
			while ((line = reader.readLine()) != null){
				content += line +"n";
			}
			
			Toast.makeText(getApplicationContext(), 
                                  "Read from Raw file: n" + content, Toast.LENGTH_SHORT).show();
			
			//WRITE (app file)
			FileOutputStream fs = openFileOutput("my_app_file", Context.MODE_APPEND);
			fs.write((content+"n").getBytes());
			fs.close();
			
			Toast.makeText(getApplicationContext(), "File writted", Toast.LENGTH_SHORT).show();

		}
		
		public void readAppFile (View view) throws Exception {
			
			String output = "";
					
			FileInputStream fd = openFileInput("my_app_file");
			BufferedReader reader = new BufferedReader(new InputStreamReader(fd));
			if (fd != null) {
				String line = null;
				while ((line = reader.readLine()) != null){
					output += line +"n";
				}
			}
			
			Toast.makeText(getApplicationContext(), 
                                  "Read from app file: n" + output, Toast.LENGTH_SHORT).show();
		}
		
		public void resetAppFile (View view) throws Exception {
			
			FileOutputStream fs = openFileOutput("my_app_file", Context.MODE_PRIVATE);
			fs.write("".getBytes());
			fs.close();
			
			Toast.makeText(getApplicationContext(), 
                                   "App File reseted", Toast.LENGTH_SHORT).show();
		}
		
	    
	}


http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
http://stackoverflow.com/questions/5771366/reading-a-simple-text-file