Working with Files
2009-04-16 16:03
232 查看
What you learn: You will learn how to access file (read and write) and why we cannot use standard Java file-access.
Problems/Questions: Write them right below...
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:
, 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
apps, that i.e. want to manipulate the files we created in a bad manner (Like writing into them: "Whos reads this is... dumb ! Hhahaha").
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).
Have a more detailed read about the whole Security of Android here.
1.Writing a file...
2.Reading the file back...
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.
You delete a file with the following method coming from ApplicationContext:
Problems/Questions: Write them right below...
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:
Java: |
FileWriter f = new FileWriter("impossible.txt"); // throws: 'java.io.FileNotFoundException: /impossible.txt ' |
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
apps, that i.e. want to manipulate the files we created in a bad manner (Like writing into them: "Whos reads this is... dumb ! Hhahaha").
Note wrote: | ||
But, writing to SD-Cards is still possible with 'normal' Java methods
|
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).
Have a more detailed read about the whole Security of Android here.
1.Writing a file...
Java: |
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(); |
Java: |
// ##### 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(); } |
"/data/data/your_project_package_structure/files/samplefile.txt"
How to browse the FileSystem of the Emulator watch 3 posts below.
You delete a file with the following method coming from ApplicationContext:
Java: |
public boolean deleteFile(String name) // Delete the given private file associated with this Context's application package. |
相关文章推荐
- 431.chapter10. working with flat files
- #3 working with data stored in files && securing your application
- Working with CSV files in Bash
- Working with Excel Files in Python
- Section 5: Working with Files and Directories
- Steps to developing Metro style apps 第四章(1) Working with data and files
- Working with Files(Chapter 16 of Programming in Objective-C 2.0)
- Working with PDF files in C# using PdfBox and IKVM
- Working with memory mapped files in .NET 4
- Working with Linux Files and Folders in Bash
- Working with Terracotta Configuration Files
- Working with iPhone files and folders
- Introduction to DataSets and working with XML files
- Working with Configuration Files (app.config) in C++/CLI
- Working with Files in C#
- VBscript (一) Working with files
- SAP DEMO-Working With Files
- Working with Configuration Files (app.config) in C++/CLI
- Linux: working with text files
- Working-with-files-in-javascript