Java IllegalArgumentException:文件包含路径分隔符

Java IllegalArgumentException:文件包含路径分隔符,java,android,file,exception,illegalargumentexception,Java,Android,File,Exception,Illegalargumentexception,我正在尝试写入HTC One上的输出文件,并在LogCat中获得以下消息: 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){ File patternDir

我正在尝试写入HTC One上的输出文件,并在LogCat中获得以下消息:

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){

    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();
    }

如果有人能帮我找出问题,我将不胜感激。

openFileInput方法将不接受路径分隔符。(“/”)

它只接受要打开/访问的文件的名称。所以,改变这个说法

outputStream = openFileOutput(patternDirectory.getAbsolutePath().toString(), Context.MODE_APPEND);


一个问题可能是您确实:
Environment.getExternalStorageDirectory().getAbsolutePath().toString()+“/com.example.pattern1/myfile.txt”

您创建了一个名为myfile.txt的目录,该目录可能与@Talal Saleem重复,哪一行给出了错误?@timrau您的链接是输入案例,这是关于输出的。小调,我知道。谢谢你的回答。我不知道如果你没有贴出来,我怎么会得到这个答案。否则,我无法保存到位于内部文件目录(getFilesDir())中的文件。我很困惑,为什么谷歌所谓的使用openFileOutput的文档化方法不起作用。非常混乱的谷歌文档(,int))。
outputStream = new FileOutputStream (new File(patternDirectory.getAbsolutePath().toString()), true); // true will be same as Context.MODE_APPEND