java 在安卓上打开文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11712336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Opening file on android
提问by hidura
i want to open a file that i create in android and allways give me this error, the main problem is when i want to open the file, that the app crash
我想打开我在 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)
This is the code to save the file.
这是保存文件的代码。
//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();
This is the code to open the file.
这是打开文件的代码。
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();
回答by Sparky
You are not specifying the path correctly when you open the file for read. The constructor you use for Filerequires a full path, not just a file name.
打开文件进行读取时,您没有正确指定路径。您用于File的构造函数需要完整路径,而不仅仅是文件名。
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.
如果你用openFileOutput()写文件,那么我建议你用openFileInput()读回来。这样您更有可能获得所有参数。
For more guidance on reading and writing files, see here: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
有关读取和写入文件的更多指导,请参见此处: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.
此外,您的文件名在读取和写入时是不同的。您应该在字符串资源中或至少在静态 final 中定义它。