http://blog.csdn.net/alex_zhuang/article/details/7340901
For the following error:
Java.lang.runtimeexception:java.lang.illegalargumentexception:file/data/data/com.alex.datasave/files/user.txt Contains a path separator
Original code:
FIS = this.context.openFileInput ("/data/data/com.alex.datasave/files/user.txt");
Correct code:
File File = new file ("/data/data/com.alex.datasave/files/user.txt");
FIS = new FileInputStream (file);
Attention:
1.FileInputStream and Openfileinput
Additional knowledge:
Public abstract FileOutputStream openfileoutput (String name, int mode) SINCE:API Level 1
Open a private file associated with the This Context ' s application package for writing. Creates the file if it doesn ' t already exist.
Parameters
Name |
The name of the file to open; Can not contain path separators. |
Mode |
Operating mode. Use 0 or MODE_PRIVATE for the default operation, to MODE_APPEND append to a existing file, and to MODE_WORLD_READABLE MODE_WORLD_WRITEABLE control permissions. |
Returns
- FileOutputStream resulting output stream.
If you want to open a file stored in the/data/data/<package name>/files directory app, you can use the activity to provide the Openfileinput () method.
FileInputStream instream = This.getcontext (). Openfileinput ("User.txt");
LOG.I ("Filetest", Readinstream (instream));
Readinstream () Please see the following remarks on this page.
or use the absolute path of the file directly:
File File = new file ("/data/data/com.alex.datasave/files/user.txt");
FileInputStream instream = new FileInputStream (file);
LOG.I ("Filetest", Readinstream (instream));
Note: "Com.alex.datasave" in the file path above is the package that the app is in, and when you write the code you should replace it with the package used by your own app.
For private files can only be accessed by the app that created the file, if you want the file to be read and written by another app, you can specify context.mode_world_readable and Context.mode_world_writeable permissions when you create the file
[Zhuan] Android Exception Handling: Java.lang.IllegalArgumentException (... contains a path separator)