0

我想打开一个我在android中创建的文件,总是给我这个错误,主要问题是当我想打开文件时,应用程序崩溃

07-29 14:56:09.708: E/AndroidRuntime(577): java.lang.IllegalStateException: Could not execute method of the activity
07-29 14:56:09.708: E/AndroidRuntime(577):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-29 14:56:09.708: E/AndroidRuntime(577):  at java.lang.reflect.Method.invokeNative(Native Method)
07-29 14:56:09.708: E/AndroidRuntime(577):  at java.lang.reflect.Method.invoke(Method.java:511)
07-29 14:56:09.708: E/AndroidRuntime(577):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-29 14:56:09.708: E/AndroidRuntime(577):  at dalvik.system.NativeStart.main(Native Method)
07-29 14:56:09.708: E/AndroidRuntime(577):  at java.lang.reflect.Method.invokeNative(Native Method)
07-29 14:56:09.708: E/AndroidRuntime(577):  at java.lang.reflect.Method.invoke(Method.java:511)
07-29 14:56:09.708: E/AndroidRuntime(577):  at menu.menuapp.FrontActivity.confirm(FrontActivity.java:51)
07-29 14:56:09.708: E/AndroidRuntime(577):  at libcore.io.Posix.open(Native Method)

这是保存文件的代码。

//Opening the notes file to be write.
    String notes_str = "<resource></resource>"
FileOutputStream notes_file = openFileOutput("notes.xml", Context.MODE_PRIVATE);

//Converting to string the notes_doc
String notes_str = new menu.menuapp.general().xmltostring(notes_doc);
//Saving on the file
notes_file.write(notes_str.getBytes());
//Closing the file.
notes_file.close();

这是打开文件的代码。

File file = new File("notes.xml");
FileInputStream notes_xml = new FileInputStream(file);
byte fileContent[] = new byte[(int)notes_xml.available()];
//The information will be content on the buffer.

notes_xml.read(fileContent);

String strContent = new String(fileContent);
    notes_xml.close();
4

1 回答 1

3

You are not specifying the path correctly when you open the file for read. The constructor you use for File requires a full path, not just a file name.

If you write the file with openFileOutput(), then I suggest you read it back with openFileInput(). That way you're more likely to get all the parameters right.

For more guidance on reading and writing files, see here: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

Also, your filename is different when reading and writing. You should define that in a string resource or at least a static final.

于 2012-07-29T19:51:04.997 回答