如何删除目录Java Android应用程序中的所有文件?当前代码正在删除该文件夹 [英] How to delete all files in a directory java android app? Current code is deleting the that folder

查看:179
本文介绍了如何删除目录Java Android应用程序中的所有文件?当前代码正在删除该文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

private final static String Dir = "/data/data/org.xbmc.kodi/cache/";
private void deleteDir(){
    try{
        /*File dir = Utils.getCacheDir(null, this, false, true);
        File path = new File(dir.getParentFile(), Dir);
        delete(path);*/
        String command = "rm -r "+Dir;
        Process process = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(process.getOutputStream());
        os.writeBytes(command + "\n");
        os.writeBytes("exit\n");
        os.flush();
        process.waitFor();
    }catch(Exception e){}
}

问题是,当我运行此命令时,它将删除该文件夹.但是我不想删除该文件夹.我要删除其中的所有文件.

The issue is when I run this it deletes the folder. But I don't want to delete the folder. I want to delete all the files inside it.

推荐答案

File dir = new File(Environment.getExternalStorageDirectory()+"Dir_name_here"); 
if (dir.isDirectory()) 
{
    String[] children = dir.list();
    for (int i = 0; i < children.length; i++)
    {
       new File(dir, children[i]).delete();
    }
}

源线程.

这篇关于如何删除目录Java Android应用程序中的所有文件?当前代码正在删除该文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆