Questions about Saving/Accessing saved files in Android

All right, i'm reading a lot about the saving and accessing implementations in various Android and off-site documentations/help sites, but i still can't understand the implementation and how does it work, so my last resort is to turn to StackOverFlow (i'm working on this on my own)

I'm going to sound a bit daft and retarded at times in this question because i'm learning on-the-fly whilst making my application, so bear with me (and i've labelled out parts of the entire document where i have questions):

First off, i see that to implement a saved file, one has to write (as taken from android docs):

//Declarations
String FILENAME = "hello_file";
String string = "hello world!";

//Meaning that FILENAME is to be saved as hello_file, and "hello world!" converts the string to bytes
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

inside whichever function that saves the data (example a button) inside, like this:

public void testButtonPressToSave() {

FileOutputStream testSaveFile = openFileOutput(SavedFile, Context.MODE_PRIVATE);
testSaveFile.write();
testSaveFile.close();

}

However, when i implement it into the code, Eclipse recommends me to use a try/catch exception in the openFileOutput part, and the entire thing changes to:

public void testButtonPressToSave() {
    FileOutputStream testSaveFile;
try {
    testSaveFile = openFileOutput(SavedFile, Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
try {
    testSaveFile.write(testString.getBytes());
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
try {
    testSaveFile.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}

(QUESTION) Is this normal? How am i able to reuse my saved file if it is on another activity? Do i import the entire main activity into the, say, a widget provider class?

Also, I fully understand that:

1)  FileOutputStream testSaveFile; and testSaveFile = openFileOutput(SavedFile, Context.MODE_PRIVATE);

Declares testSaveFile as a FileOutputStream object and that it saves a file (.txt?) using a Context.MODE_PRIVATE which limits the file to only being able to be accessed by the application itself.

and,

2)  testSaveFile.close(); 

Ends the stream, somehow (but this is pretty direct, it just closes the file)

The part i don't really get is, how do i save multiple variables inside the SavedFile data package?

The Android documentation provides me with the available write() functions under FileOutputStream:

public void write (byte[] buffer, int offset, int byteCount)

public void write (byte[] buffer)

public void write (int oneByte)

Which isnt exactly what i want, because i need the stream to save multiple variables such as a String and Integer[].

(QUESTION) How do i go about saving my desired data types into the SavedFile?

I've also read about Serialization, but i'm unsure how it would actually work to save a file into my application. Also, i'm not very confident that Serialization would be very efficient on a Dalvik VM (Android) because most of the code i've read and gone through are based on Java systems.

There was also a Bundle android resource thingy that i don't understand but seems like the answer to store various multiple variables into one package and then unpackaging them at the next activity, although i don't see how i am able to actually save it into a file or something.

Alright, i'm blabbing away with a lot of points but i'd be very grateful if anyone would be able to answer these questions. You don't have to provide an answer, but a clear explanation would be very much appreciated (especially around technical jargons :S)


Looks like i've found a more effective way to store my data, through SQLiteDatabase in Android. Thanks anyway!

链接地址: http://www.djcxy.com/p/90012.html

上一篇: 在android中保存数据(使用processing.core)

下一篇: 有关在Android中保存/访问保存的文件的问题