Android File Writing to Internal Storage
A special Note about where the file is located and other apps reading your file
Note: Your app's internal storage directory is specified by your app's package name in a special location of the Android file system. Technically, another app can read your internal files if you set the file mode to be readable. However, the other app would also need to know your app package name and file names. Other apps cannot browse your internal directories and do not have read or write access unless you explicitly set the files to be readable or writable. So as long as you use MODE_PRIVATE for your files on the internal storage, they are never accessible to other apps.
REGARDING MODE_WORLD_* = suggestion is to NOT use
int MODE_PRIVATE File creation mode: the default mode, where the created file can only be accessed by the calling application (or all applications sharing the same user ID). int MODE_WORLD_READABLE This constant was deprecated in API level 17. Creating world-readable files is very dangerous, and likely to cause security holes in applications. It is strongly discouraged; instead, applications should use more formal mechanism for interactions such asContentProvider, BroadcastReceiver, and Service. There are no guarantees that this access mode will remain on a file, such as when it goes through a backup and restore. File creation mode: allow all other applications to have read access to the created file. int MODE_WORLD_WRITEABLE This constant was deprecated in API level 17. Creating world-writable files is very dangerous, and likely to cause security holes in applications. It is strongly discouraged; instead, applications should use more formal mechanism for interactions such asContentProvider, BroadcastReceiver, and Service. There are no guarantees that this access mode will remain on a file, such as when it goes through a backup and restore. File creation mode: allow all other applications to have write access to the created file.
Step 1: Use java.io.FileOutputStream class to open file, use Activities openFileOutput(*,*) method from Activity class
//Inside your Activity class
FileOutputStream fOut = openFileOutput("myFile.txt", MODE_WORLD_READABLE);
Other Modes: MODE_APPEND, MODE_WORLD_WRITEABLE
Step 2: OPTIONAL convert your FileOutputStream instance of step 1 to a easier to use java.io class
OutputStreamWriter osw = new OutputSteramWriter (fOut);
Step 3: Write , Flush and Close when done
osw.write("string");
osw.flush();
osw.close();
Android File Reading from Internal Storage
Step 1: Use java.io.FileInputStream class to open file using openFileInput(*) method from Activity class
//inside Activity class
FileInputStream fIn = openFileInput("myFile.txt");
Step 2: OPTIONAL convert your FileOutputStream instance of step 1 to a easier to use java.io class
InputStreamReader isw = new InputStreamReader (fIn);
Step 3: Read and Close when done
int charRead = isw.read();
iws.close();
EXAMPLE: writing and reading to file in Internal Storage
main.xml App's Activity layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Please enter some text" />
<EditText android:id="@+id/txtText1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btnSave" android:text="Save" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClickSave" />
<Button android:id="@+id/btnLoad" android:text="Load" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClickLoad" /> </LinearLayout>
App's Activity
public class FilesActivity extends Activity { EditText textBox; static final int READ_BLOCK_SIZE = 100;
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textBox = (EditText) findViewById(R.id.txtText1); } //CALLED when button asking to Save text user types into textBox is hit public void onClickSave(View view) { String str = textBox.getText().toString(); try { //---SD Card Storage--- STEP 1: Open up OutputStream to File textfile.txt FileOutputStream fOut = openFileOutput("textfile.txt", MODE_WORLD_READABLE); //STEP 2: Create OutputStreamWriter from FileOutputStream OutputStreamWriter osw = new OutputStreamWriter(fOut); //---write the string to the file--- STEP 3 write, flush, close osw.write(str); osw.flush(); osw.close(); //---display file saved message--- Toast.makeText(getBaseContext(), "File saved successfully!",Toast.LENGTH_SHORT).show(); //---clears the EditText--- textBox.setText(""); } catch (IOException ioe) { ioe.printStackTrace(); } } //CALLED when button asking to Load text user types into textBox is hit public void onClickLoad(View view) { try { //---SD Storage--- //STEP 1 Opening to Read from textfile.txt FileInputStream fIn = openFileInput("textfile.txt"); InputStreamReader isr = new InputStreamReader(fIn); char[] inputBuffer = new char[READ_BLOCK_SIZE]; String s = ""; int charRead; while ((charRead = isr.read(inputBuffer))>0) //STEP 3: read from file until eof, -1 returned { //---convert the chars to a String--- String readString = String.copyValueOf(inputBuffer, 0,charRead); s += readString;
inputBuffer = new char[READ_BLOCK_SIZE]; } //---set the EditText to the text that has been // read--- textBox.setText(s); Toast.makeText(getBaseContext(), "File loaded successfully!", Toast.LENGTH_SHORT).show(); } catch (IOException ioe) { ioe.printStackTrace(); } } }