【问题标题】:Delete a folder if it exists and write it as a new删除一个文件夹(如果存在)并将其写入一个新文件夹
【发布时间】:2013-12-03 03:19:07
【问题描述】:

我有这个代码来创建一个文件夹:

 File folder = new File(Environment.getExternalStorageDirectory() + "/Naruto Generation/");
                boolean success = false;
                if (!folder.exists()) {
                    success = folder.mkdirs();
                }
                if (!success) {
                } else {
                }



                File direct = new File("/sdcard/Naruto Generation/");

                if (!direct.exists()) {
                    direct.mkdirs();
                }

现在我想更改它,并创建文件夹,但如果文件夹存在,请将其删除并将其写入新的。

我该怎么做?提前致谢

【问题讨论】:

    标签: android file save directory


    【解决方案1】:

    要删除一个目录,你必须删除它的所有内容:

    例如:

    if (folder.isDirectory()) {
       File[] children = folder.listFiles();
       for (File child : children) {
          child.delete();
       }
    
       folder.delete();
    }
    

    【讨论】:

      猜你喜欢
      • 2018-05-25
      • 2020-02-19
      • 1970-01-01
      • 2011-01-19
      • 1970-01-01
      • 2010-11-07
      • 2023-03-30
      • 1970-01-01
      相关资源
      最近更新 更多