Writing to a File On Device
Uses read and write methods to a file, also uses BufferedReader & BufferedWriter
Opens a private file for writing, creates that file,
Checks whether file exists, if yes opens it, if no creates that file
Text is red from file and displayed
package course.examples.DataManagement.FileInternal;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;
import course.examples.Files.FileWriteAndRead.R;
//Extend Activity
public class InternalFileWriteReadActivity extends Activity {
//String fileName is the writeable file the system looks for and opens
//If does not exists, it creates
private final static String fileName = "TestFile.txt";
private String TAG = "InternalFileWriteReadActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//LinearLayout created that will be later passed into the readFile m() to display the text
LinearLayout ll = (LinearLayout) findViewById(R.id.linearLayout);
// Check whether fileName already exists in directory used
// by the openFileOutput() method.
// If the text file doesn't exist, then create it now
if (!getFileStreamPath(fileName).exists()) {
try {
writeFile();
} catch (FileNotFoundException e) {
Log.i(TAG, "FileNotFoundException");
}
}
// Read the data from the text file and display it
try {
readFile(ll);
} catch (IOException e) {
Log.i(TAG, "IOException");
}
}
private void writeFile() throws FileNotFoundException {
FileOutputStream fos = openFileOutput(fileName, MODE_PRIVATE);
PrintWriter pw = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(fos)));
pw.println("Line 1: This is a test of the File Writing API");
pw.println("Line 2: This is a test of the File Writing API");
pw.println("Line 3: This is a test of the File Writing API");
pw.close();
}
private void readFile(LinearLayout ll) throws IOException {
FileInputStream fis = openFileInput(fileName);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = "";
while (null != (line = br.readLine())) {
TextView tv = new TextView(this);
tv.setTextSize(24);
tv.setText(line);
ll.addView(tv);
}
br.close();
}
}
=======================================================================
// XML linear layout
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>