Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.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
已在android sd卡上创建文件夹,但未创建文件_Android_File Io - Fatal编程技术网

已在android sd卡上创建文件夹,但未创建文件

已在android sd卡上创建文件夹,但未创建文件,android,file-io,Android,File Io,我想我已经看了所有相关的问题,但我仍然无法让它发挥作用 代码如下: File sdCard = Environment.getExternalStorageDirectory(); File directory= new File (sdCard.getAbsolutePath() + appName); directory.mkdirs(); File file = new File(directory,fileName); 文件夹已创建,但我收到一个错

我想我已经看了所有相关的问题,但我仍然无法让它发挥作用

代码如下:

File sdCard = Environment.getExternalStorageDirectory();  
File directory= new File (sdCard.getAbsolutePath() + appName);          
directory.mkdirs();     
File file = new File(directory,fileName);  
文件夹已创建,但我收到一个错误消息,说该文件不存在。appName是一个包含文件夹名称的字符串,可以正常工作。fileName是一个字符串,包含我要包含的文件名

我已经在舱单上写上了许可

我做错了什么

更新:

代码试图同时创建子目录和文件,这是隐藏的,因为代码使用命名字符串而不是字符串文字。添加创建子目录的中间步骤解决了此问题。

请尝试以下操作:

在这里,我创建了一个字符串的文本文件(.txt文件)

public void createFileFromString(String text)
    {       
       File logFile = new File("sdcard/xmlresponseiphone.txt");
       if (!logFile.exists())
       {
          try
          {
             logFile.createNewFile();
          } 
          catch (IOException e)
          {
             // TODO Auto-generated catch block
             e.printStackTrace();
          }
       }
       try
       {
          //BufferedWriter for performance, true to set append to file flag
          BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
          buf.append(text);
          buf.newLine();
          buf.close();
       }
       catch (IOException e)
       {
          // TODO Auto-generated catch block
          e.printStackTrace();
       }
    }
测试一下,看看你遗漏了什么:)

试试这个:

在这里,我创建了一个字符串的文本文件(.txt文件)

public void createFileFromString(String text)
    {       
       File logFile = new File("sdcard/xmlresponseiphone.txt");
       if (!logFile.exists())
       {
          try
          {
             logFile.createNewFile();
          } 
          catch (IOException e)
          {
             // TODO Auto-generated catch block
             e.printStackTrace();
          }
       }
       try
       {
          //BufferedWriter for performance, true to set append to file flag
          BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
          buf.append(text);
          buf.newLine();
          buf.close();
       }
       catch (IOException e)
       {
          // TODO Auto-generated catch block
          e.printStackTrace();
       }
    }

测试一下,看看你遗漏了什么:)

如果创建了目录,那么你就在正确的轨道上了。在您的代码中,实际上并不是在SD卡上创建文件。如果需要创建文件,请执行以下操作:

File sdCard = Environment.getExternalStorageDirectory();  
File file = new File(sdCard.getAbsolutePath() + appName + "/" + fileName);
directory.mkdirs();
file.createNewFile()

这只是概念上的。实际上,最好将
文件名
分为单独的子文件夹和实际文件,并分别处理它们。

如果创建了目录,那么您就走上了正确的道路。在您的代码中,实际上并不是在SD卡上创建文件。如果需要创建文件,请执行以下操作:

File sdCard = Environment.getExternalStorageDirectory();  
File file = new File(sdCard.getAbsolutePath() + appName + "/" + fileName);
directory.mkdirs();
file.createNewFile()

这只是概念上的。最好将
文件名
分为单独的子文件夹和实际文件,并分别处理它们。

尝试类似的方法。在这种情况下,我正在保存一个图像

要创建目录,请执行以下操作:

File directory = new File(Environment.getExternalStorageDirectory()
                + File.separator + appName);
        directory.mkdirs();
为了省钱 公共无效保存(位图图形、上下文上下文、字符串名称、字符串时间、布尔值)引发IOException{

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    graph.compress(Bitmap.CompressFormat.PNG, 100, bytes);

    // you can create a new file name "test.jpg" in sdcard folder.
    String fileName = "";
    if (now){
        fileName = getDateTime()+"_00"+".png";
    }
    else {
        fileName = time.replace(".txt", ".png");
    }
    File f = new File(Environment.getExternalStorageDirectory()
            + File.separator + "appName/" + fileName);
    f.createNewFile(); // write the bytes in file
    FileOutputStream fo = new FileOutputStream(f);
    fo.write(bytes.toByteArray());
}

我认为诀窍在于File.separator!

尝试使用类似的方法。在这种情况下,我正在保存一个图像

要创建目录,请执行以下操作:

File directory = new File(Environment.getExternalStorageDirectory()
                + File.separator + appName);
        directory.mkdirs();
为了省钱 公共无效保存(位图图形、上下文上下文、字符串名称、字符串时间、布尔值)引发IOException{

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    graph.compress(Bitmap.CompressFormat.PNG, 100, bytes);

    // you can create a new file name "test.jpg" in sdcard folder.
    String fileName = "";
    if (now){
        fileName = getDateTime()+"_00"+".png";
    }
    else {
        fileName = time.replace(".txt", ".png");
    }
    File f = new File(Environment.getExternalStorageDirectory()
            + File.separator + "appName/" + fileName);
    f.createNewFile(); // write the bytes in file
    FileOutputStream fo = new FileOutputStream(f);
    fo.write(bytes.toByteArray());
}

我想诀窍在于File.separator!

谢谢你的回答。我添加了一行代码,但仍然得到了这些异常。03-09 09:56:42.697:W/System.err(31125):java.io.IOException:没有这样的文件或目录03-09 09:56:42.707:W/System.err(31125):在java.io.File.createNewFileImpl(本机方法)上@user1244332 appName和filename的值是什么?sdCard.getAbsolutePath()返回什么?appName=“/JoesHouse.filename=“default/text.txt”。sdCard.getAbsolutePath()返回/mnt/sdCard.directory.getAbsolutePath()returns/mnt/sdcard/JoesHouse。出于好奇,我尝试使用Quickoffice将文件添加到文件夹中,结果成功,因此可以将文件夹写入。@user1244332您的“文件名”包含另一个目录名-但该目录不存在。我已更新了我的答案,以显示您应该如何执行所需操作。感谢您的回答。我添加了一行代码,但仍然会出现这些异常。03-09 09 09:56:42.697:W/System.err(31125):java.io.IOException:没有这样的文件或目录03-09 09:56:42.707:W/System.err(31125):在java.io.File.createNewFileImpl(Native Method)@user1244332,您的appName和filename值是多少?sdCard.getAbsolutePath()返回什么?appName=“/JoesHouse.filename=”default/text.txt“。sdCard.getAbsolutePath()返回/mnt/sdCard.directory.getAbsolutePath()returns/mnt/sdcard/JoesHouse。出于好奇,我尝试使用Quickoffice将文件添加到文件夹中,结果成功,因此可以将文件夹写入。@user1244332您的“文件名”包含另一个目录名-但该目录不存在。我已更新了我的答案,以显示您应该如何执行所需操作。如果您在windows中工作,请为Aleks G answer添加两个//而不是一个!Eclipse在windows上,但我正在手机上调试。一个/应该可以--对吗?请尝试使用类似以下内容:File f=new File(Environment.getExternalStorageDirectory()+File.separator+“appName”);f.createNewFile();我认为File.separator会起作用!我将完整代码作为答案!这里很难看到!如果您在windows中工作,请为Aleks G answer添加两个//而不是一个!Eclipse在windows上,但我正在手机上调试。一个/应该可以--对吧?尝试使用类似的方式:File f=new File(Environment.getExternalStorageDirectory()+File.separator+“appName”);f.createNewFile();我认为File.separator起作用!我把完整的代码作为答案!这里很难看到!谢谢。我在创建文件的过程中尝试创建目录。我对java IOT非常陌生谢谢。我在创建文件的过程中尝试创建目录。我对java IO非常陌生