Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android中用SDcard编写文件_Java_Android - Fatal编程技术网

Java 在Android中用SDcard编写文件

Java 在Android中用SDcard编写文件,java,android,Java,Android,我一直在寻找这个问题。 它实际上在旧版本的android中工作,但在我更新SDK之后,我得到了一个错误。 错误消息已打开:ENOTDIR不是目录:/sdcard/PlayNumbers/mysdfile.xml 有人能告诉我我做错了什么吗?? 我的代码如下 非常感谢, path=new File("/sdcard/PlayNumbers"); myFile = new File(path,"mysdfile.xml"); if (!path.exists()) { path.mkdirs

我一直在寻找这个问题。 它实际上在旧版本的android中工作,但在我更新SDK之后,我得到了一个错误。 错误消息已打开:ENOTDIR不是目录:/sdcard/PlayNumbers/mysdfile.xml 有人能告诉我我做错了什么吗?? 我的代码如下

非常感谢,

path=new File("/sdcard/PlayNumbers");
myFile = new File(path,"mysdfile.xml");
if (!path.exists()) {
    path.mkdirs();
}
if(!myFile.exists()){
    myFile.createNewFile();
}

FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);

myOutWriter.append("test");
myOutWriter.close();
fOut.close();
==>


p、 好的,我已经像你们提到的那个样修改了我的代码,但它仍然给我同样的错误,它不是目录。。。有什么想法吗?

您只需要更改以下代码,因为您缺少/:

myFile = new File(path,"/mysdfile.xml");
但请记住,您必须拥有在清单文件的外部存储中写入的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

您只需更改为以下代码,因为缺少/:

myFile = new File(path,"/mysdfile.xml");
但请记住,您必须拥有在清单文件的外部存储中写入的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

假设您在清单中具有正确的权限,则此操作应该有效:

File externalStorageDir = Environment.getExternalStorageDirectory();
File playNumbersDir = new File(externalStorageDir, "PlayNumbers");
File myFile = new File(playNumbersDir, "mysdfile.xml");

if (!playNumbersDir.exists()) {
    playNumbersDir.mkdirs();
}
if(!myFile.exists()){
    myFile.createNewFile();
}

假设您在清单中具有正确的权限,则此操作应该有效:

File externalStorageDir = Environment.getExternalStorageDirectory();
File playNumbersDir = new File(externalStorageDir, "PlayNumbers");
File myFile = new File(playNumbersDir, "mysdfile.xml");

if (!playNumbersDir.exists()) {
    playNumbersDir.mkdirs();
}
if(!myFile.exists()){
    myFile.createNewFile();
}

你好,Husam A.Al-ahmadi,我已经尝试过了,但是它仍然给出了ENOTDIR错误mnt/sdcard/PlayNumbers/mysdfile.xml。另外,我有外部存储权限。好的,JMax,我有/。我写的是path=newfilepath+/PlayNumbers;。。我还是会出错。我不确定我做错了什么你好Husam A.Al ahmadi,我已经试过了,但它仍然给出了ENOTDIR错误mnt/sdcard/PlayNumbers/mysdfile.xml。另外,我有外部存储权限。好的,JMax,我有/。我写的是path=newfilepath+/PlayNumbers;。。我还是会出错。我不确定我做错了什么