Android java.lang.IllegalArgumentException:包含路径分隔符
我的代码中有一个文件名:Android java.lang.IllegalArgumentException:包含路径分隔符,android,Android,我的代码中有一个文件名: 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";
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";
openFileInput()
不接受路径,只接受文件名
如果要访问路径,请使用
File File=new File(path)
和相应的FileInputStream
此方法在应用程序的私有数据区域中打开一个文件。您不能使用此方法打开此区域的子目录中的任何文件,或完全从其他区域打开任何文件。因此,直接使用FileInputStream
的构造函数传递包含目录的路径 不能直接将path与目录分隔符一起使用,但可以
必须为每个目录创建一个文件对象
注意:此代码生成目录,您可能不需要该目录
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);
File File=context.getFilesDir();
mkdir()文件;
String[]数组=filePath.split(“/”);
for(int t=0;t
解决方案是:
FileInputStream fis = new FileInputStream (new File(NAME_OF_FILE)); // 2nd line
openFileInput方法不接受路径分隔符
别忘了
fis.close();
在末尾。文件File=context.getFilesDir();
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);
mkdir()文件;
String[]数组=filePath.split(“/”);
for(int t=0;t 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
}
}
我在尝试使用带有子目录
Dir
的openFileInput(“/Dir/data.txt”)
方法从内部存储器访问文件时收到上述错误消息
您不能使用上述方法访问子目录
尝试以下方法:
FileInputStream fIS = new FileInputStream (new File("/Dir/data.txt"));
什么<代码>文件f=新文件(fileDirPath);f、 mkdirs()请编辑既然可以这样使用
FileInputStream
,为什么选择使用openFileInput
?这应该是正确的答案,因为它提供了一个清晰的示例。简单的解决方案是最好的。谢谢。请提供一些例子,你的答案令人困惑;OP正在使用FileInputStreamProvider示例请详细说明答案。这是错误的答案-请参见下面的答案与上一个答案相同。
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());
}