异常一:
java.lang.IllegalArgumentException: File /mnt/sdcard/leichi.txt contains a path separatorFile
出错的代码:
String path = Environment.getExternalStorageDirectory().toString();
String fileNamePath = path +File.separatorChar+"srt"+File.separatorChar+fileName;
FileOutputStream fileOutputStream = context.openFileOutput(fileNamePath, Context.MODE_APPEND);
fileOutputStream.write(msg.getBytes());
fileOutputStream.close();
正常的代码:
String path = Environment.getExternalStorageDirectory().toString();
String fileNamePath = path +File.separatorChar+"srt"+File.separatorChar+fileName;
File file = new File(fileNamePath);
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(msg.getBytes());
fileOutputStream.close();
原因:
1.FileInputStream
补充知识:
public abstract FileOutputStream openFileOutput (String name,int mode)
Open a private file associated with this Context's applicationpackage for writing. Creates the file if it doesn't alreadyexist.
Parameters
name | The name of the file to open; can not contain pathseparators. |
---|---|
mode | Operating mode. Use 0 or MODE_PRIVATE MODE_APPEND MODE_WORLD_READABLE MODE_WORLD_WRITEABLE |
Returns
- FileOutputStream Resulting output stream.
如果要打开存放在/data/data/<packagename>/files目录应用私有的文件,可以使用Activity提供openFileInput()方法。
也就是说openFileInput创建的文件必须放到/data/data/<packagename>/files目录下,不能随便制定目录。