http://blog.csdn.net/alex_zhuang/article/details/7340901
对以下错误:
Java.lang.RuntimeException: java.lang.IllegalArgumentException: File /data/data/com.alex.datasave/files/user.txt contains a path separator
原先代码:
fis = this.context.openFileInput("/data/data/com.alex.datasave/files/user.txt");
正确代码:
File file = new File("/data/data/com.alex.datasave/files/user.txt");
fis = new FileInputStream(file);
注意:
1.FileInputStream 与 openFileInput
补充知识:
public abstract FileOutputStream openFileOutput (String name, int mode)
Open a private file associated with 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, MODE_APPEND to append to an existing file, MODE_WORLD_READABLE andMODE_WORLD_WRITEABLE to control permissions. |
如果要打开存放在/data/data/<package name>/files目录应用私有的文件,可以使用Activity提供openFileInput()方法。
FileInputStream inStream = this.getContext().openFileInput("user.txt");
Log.i("FileTest", readInStream(inStream));
readInStream()的方法请看本页下面备注。
或者直接使用文件的绝对路径:
File file = new File("/data/data/com.alex.datasave/files/user.txt");
FileInputStream inStream = new FileInputStream(file);
Log.i("FileTest", readInStream(inStream));
注意:上面文件路径中的“com.alex.datasave”为应用所在包,当你在编写代码时应替换为你自己应用使用的包。
对于私有文件只能被创建该文件的应用访问,如果希望文件能被其他应用读和写,可以在创建文件时,指定Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE权限