您的位置:首页 > 其它

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:

Java:
FileWriter f = new FileWriter("impossible.txt");

// throws: 'java.io.FileNotFoundException: /impossible.txt '
, 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").

Note wrote:
But, writing to SD-Cards is still possible with 'normal' Java methods


Java:
FileWriter f = new FileWriter("/sdcard/download/possible.txt");
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).

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();
2.Reading the file back...

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();

}
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:

Java:
public boolean deleteFile(String name)

// Delete the given private file associated with this Context's application package.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: