IllegalArgumentException:文件包含路径分隔符Android [英] IllegalArgumentException: File contains a path separator Android
问题描述
$ b
11- 21 08:05:18.228:W / System.err(6609):java.lang.IllegalArgumentException:File /storage/emulated/0/com.example.pattern1/myfile.txt包含路径分隔符
源代码如下:
protected void writeToFile (String string){
文件patternDirectory = new File(Environment.getExternalStorageDirectory()。getAbsolutePath()。toString()+/ com.example.pattern1 / myfile.txt);
patternDirectory.mkdirs();
FileOutputStream outputStream;
try {
outputStream = openFileOutput(patternDirectory.getAbsolutePath()。toString(),Context.MODE_APPEND);
outputStream.write(string.getBytes());
TextView t =(TextView)findViewById(R.id.bottomMidText);
t.setText(patternDirectory.getAbsolutePath()。toString());
outputStream.close();
catch(Exception e){
e.printStackTrace();
}
如果有人能够帮助识别问题,我将不胜感激。 b $ b
openFileInput方法不会接受路径分隔符('/')
只接受您要打开/访问的文件的名称。所以改变语句
outputStream = openFileOutput(patternDirectory.getAbsolutePath()。toString(),Context.MODE_APPEND);
到
outputStream = new FileOutputStream(new File(patternDirectory.getAbsolutePath()。toString()),true); // true将与Context.MODE_APPEND相同
I'm trying to write to an output file on my HTC One and get the following message in the LogCat:
11-21 08:05:18.228: W/System.err(6609): java.lang.IllegalArgumentException: File /storage/emulated/0/com.example.pattern1/myfile.txt contains a path separator
The source code is given below:
protected void writeToFile(String string){
File patternDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString()+"/com.example.pattern1/myfile.txt");
patternDirectory.mkdirs();
FileOutputStream outputStream;
try {
outputStream = openFileOutput(patternDirectory.getAbsolutePath().toString(), Context.MODE_APPEND);
outputStream.write(string.getBytes());
TextView t = (TextView)findViewById(R.id.bottomMidText);
t.setText(patternDirectory.getAbsolutePath().toString());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
I would appreciate if someone can help identify the problem.
The openFileInput method will not accept path separators.('/')
it accepts only the name of the file which you want to open/access. so change the statement
outputStream = openFileOutput(patternDirectory.getAbsolutePath().toString(), Context.MODE_APPEND);
to
outputStream = new FileOutputStream (new File(patternDirectory.getAbsolutePath().toString()), true); // true will be same as Context.MODE_APPEND
这篇关于IllegalArgumentException:文件包含路径分隔符Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!