Android POCs (Proof of Concepts)

ButtonPress-FileWriteRead

To write a file, an instance of BufferedWriter(new OutputStreamWriter(openFileOutput(filename, MODE_WORLD_WRITEABLE))) is created. To read the same file, BufferedReader(new InputStreamReader(openFileInput(filename))) is created. Only strings are used to write and read in this example. The directory path is not accessible through Eclipse DDMS navigation. As stated on http://developer.android.com/guide/topics/data/data-storage.html, "Using the 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."

download  or   install to your Android





ButtonPress-FileWriteRead/src/com/brainyideas/dev/poc/bpfwr/FileWriteRead.java
package com.brainyideas.dev.poc.bpfwr;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class FileWriteRead extends Activity {

	String dir;
	String filename = "myWritableFile";
	String eol = System.getProperty("line.separator");
    Button button1;
    String state;
	String pathSeparator;

	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

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

		init();
		
		state = "FileA";
		writeLocalFile(state);
		readFileFromLocal();
	}

	void init() {
		
		pathSeparator = System.getProperty("user.dir");
		dir = getFilesDir().getAbsolutePath();
    	
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener() {

            public void onClick(final View v) {
            	
            	if (state.equals("FileA")) {
            		state = "FileB";
            	} else {
            		state = "FileA";
            	}
        		writeLocalFile(state);
        		readFileFromLocal();
            }
        });
	}

	void writeLocalFile(String topLine) {
		
		try {
			
			BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(openFileOutput(filename, MODE_WORLD_WRITEABLE)));
			writer.write(topLine + eol);
			writer.write("line 1." + eol);
			writer.write("line 2." + eol);
			writer.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	void readFileFromLocal() {
		
		try {
			
			BufferedReader input = new BufferedReader(new InputStreamReader(openFileInput(filename)));
			String line;
			StringBuffer buffer = new StringBuffer();
			while ((line = input.readLine()) != null) {
				buffer.append(line + eol);
			}
			TextView textView = (TextView) findViewById(R.id.result);
			textView.setText("path file: " + dir + pathSeparator + filename + "\n\n" + buffer.toString());

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

ButtonPress-FileWriteRead/res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

	<Button 
		android:text="Toggle XML File"
		android:id="@+id/button1" 
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content"/>
	    
	<TextView  
	    android:layout_width="fill_parent" 
	    android:id="@+id/result"
	    android:layout_height="match_parent"/>

</LinearLayout>