Working with Files
What you learn: You will learn how to access file (read and write) and why we cannot use standard Java file-access.
Difficulty: 1 of 5

What it will look like:
This is the first tutorial without a screenshot 
Description:
To understand why we cannot use standard java file access, like:
Using java Syntax Highlighting
- FileWriter f = new FileWriter("impossible.txt");
- // throws: 'java.io.FileNotFoundException: /impossible.txt '
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
, we have to understand the Security-Model of Android.
Each *.apk File that is installed on the Emulator/Device gets its own User-ID from the Linux System. This ID is the key to the sandbox of the application. This 'sandbox' protects the application (and its files) from other bad
Note wrote:But, writing to SD-Cards is still possible with 'normal' Java methodsUsing java Syntax Highlighting
FileWriter f = new FileWriter("/sdcard/download/possible.txt");Parsed in 0.030 seconds, using GeSHi 1.0.8.4
will work without any problem, you just need to add virtual sdcard to your emulator.
Every file that you generate with your code gets is 'signed' with the User-ID of the Application that created it. One From within your app you can set flags to make the file accessible (read and/or write) for other applications with other User-IDs (Flags: MODE_WORLD_READABLE and/or MODE_WORLD_WRITEABLE).
There is a way to make two applications use the same userid (look here).
1.Writing a file...
Using java Syntax Highlighting
- try { // catches IOException below
- final String TESTSTRING = new String("Hello Android");
- // ##### Write a file to the disk #####
- /* We have to use the openFileOutput()-method
- * the ActivityContext provides, to
- * protect your file from others and
- * This is done for security-reasons.
- * We chose MODE_WORLD_READABLE, because
- * we have nothing to hide in our file */
- FileOutputStream fOut = openFileOutput("samplefile.txt",
- MODE_WORLD_READABLE);
- OutputStreamWriter osw = new OutputStreamWriter(fOut);
- // Write the string to the file
- osw.write(TESTSTRING);
- /* ensure that everything is
- * really written out and close */
- osw.flush();
- osw.close();
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
2.Reading the file back...
Using java Syntax Highlighting
- // ##### Read the file back in #####
- /* We have to use the openFileInput()-method
- * the ActivityContext provides.
- * Again for security reasons with
- * openFileInput(...) */
- FileInputStream fIn = openFileInput("samplefile.txt");
- InputStreamReader isr = new InputStreamReader(fIn);
- /* Prepare a char-Array that will
- * hold the chars we read back in. */
- char[] inputBuffer = new char[TESTSTRING.length()];
- // Fill the Buffer with data from the file
- isr.read(inputBuffer);
- // Transform the chars to a String
- String readString = new String(inputBuffer);
- // Check if we read back the same chars that we had written out
- boolean isTheSame = TESTSTRING.equals(readString);
- // WOHOO lets Celebrate =)
- Log.i("File Reading stuff", "success = " + isTheSame);
- } catch (IOException ioe) {
- ioe.printStackTrace();
- }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
The file is now located in the following folder on your emulator:
"/data/data/your_project_package_structure/files/samplefile.txt"
How to browse the FileSystem of the Emulator watch 3 posts below.
How to browse the FileSystem of the Emulator watch 3 posts below.
Using java Syntax Highlighting
- public boolean deleteFile(String name)
- // Delete the given private file associated with this Context's application package.
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
Regards,
plusminus







