java.lang.IllegalArgumentException:包含路径分隔符


79

我的代码中有一个文件名:

String NAME_OF_FILE="//sdcard//imageq.png";
FileInputStream fis =this.openFileInput(NAME_OF_FILE); // 2nd line

我在第二行出现错误:

05-11 16:49:06.355:错误/ AndroidRuntime(4570):由以下原因引起:java.lang.IllegalArgumentException:File //sdcard//imageq.png包含路径分隔符

我也尝试过这种格式:

String NAME_OF_FILE="/sdcard/imageq.png";

Answers:


58

此方法在应用程序的私有数据区域中打开一个文件。您不能使用此方法在该区域或完全其他区域的子目录中打开任何文件。因此,请FileInputStream直接使用的构造函数来传递带有目录的路径。


46
请为此提供一些示例
Anand Savjani

11
您的答案令人困惑;OP正在使用FileInputStream
FracturedRetina

7
提供示例
Lutaaya Huzaifah Idris's

2
请详细说明答案。
IgorGanapolsky

1
这是错误的答案-请参阅以下答案
巴尔

81

解决方案是:

FileInputStream fis = new FileInputStream (new File(NAME_OF_FILE));  // 2nd line

openFileInput方法不接受路径分隔符。

别忘了

fis.close();

在最后。


鉴于这样可以使用FileInputStream,为什么选择使用openFileInput
Al Lelopath

1
这应该是正确标记的答案,因为它提供了一个清晰的示例。
marienke '17

简单的解决方案是最好的。谢谢。
危险

30

openFileInput()不接受路径,仅文件名(如果要访问路径),请使用File file = new File(path)FileInputStream


39
如果您提供了一些示例代码可以实现这一点,那就太好了!
穆罕默德·巴巴尔

2
@MuhammadBabar当然,您需要代码,没有别人提供的代码就无法始终进行编码。检查文档也许?
霓虹灯Warge'1

3

尝试使用openFileInput("/Dir/data.txt")带有子目录的方法从内部存储访问文件时,收到上述错误消息Dir

您不能使用上述方法访问子目录。

尝试类似:

FileInputStream fIS = new FileInputStream (new File("/Dir/data.txt"));

2

您不能直接将路径与目录分隔符一起使用,但是必须为每个目录创建一个文件对象。

注意:此代码创建目录,您的目录可能不需要...

File file= context.getFilesDir();
file.mkdir();

String[] array=filePath.split("/");
for(int t=0; t< array.length -1 ;t++)
{
    file=new File(file,array[t]);
    file.mkdir();
}

File f=new File(file,array[array.length-1]);

RandomAccessFileOutputStream rvalue = new RandomAccessFileOutputStream(f,append);

5
什么 ?File f = new File(fileDirPath); f.mkdirs();请编辑
Mr_and_Mrs_D 2013年

1
String all = "";
        try {
            BufferedReader br = new BufferedReader(new FileReader(filePath));
            String strLine;
            while ((strLine = br.readLine()) != null){
                all = all + strLine;
            }
        } catch (IOException e) {
            Log.e("notes_err", e.getLocalizedMessage());
        }

0
File file = context.getFilesDir(); 
file.mkdir();
String[] array = filePath.split("/"); 
for(int t = 0; t < array.length - 1; t++) {
    file = new File(file, array[t]); 
    file.mkdir();
}
File f = new File(file,array[array.length- 1]); 
RandomAccessFileOutputStream rvalue = 
    new RandomAccessFileOutputStream(f, append);

与上一个答案相同。
Anis LOUNIS

0

我通过在onCreate事件中创建目录来解决这种类型的错误,然后通过在需要执行某些操作(例如在该目录中保存或检索文件)的方法中创建新的文件对象来访问目录,希望这会有所帮助!

 public class MyClass {    

 private String state;
 public File myFilename;

 @Override
 protected void onCreate(Bundle savedInstanceState) {//create your directory the user will be able to find
    super.onCreate(savedInstanceState);
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        myFilename = new File(Environment.getExternalStorageDirectory().toString() + "/My Directory");
        if (!myFilename.exists()) {
            myFilename.mkdirs();
        }
    }
 }

 public void myMethod {

 File fileTo = new File(myFilename.toString() + "/myPic.png");  
 // use fileTo object to save your file in your new directory that was created in the onCreate method
 }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.