linux java 删除文件夹_Java文件夹递归删除方法

前一段时间写程序的时候需要用Java删除一个文件夹以及文件夹下的所有功能,java文件类中的delete方法只能删除空文件夹,后来到网上搜了一下,发现有些朋友已经实现了这个功能,并且提供了源代码,于是找了一个编译测试,通过,一切顺利!但是过了一个多月发现出了问题,因为我是在Linux平台上开发,但是系统可能运行在Windows平台上。在Linux平台上运行正确,但是到Windows平台上以后,发现提示用户删除文件成功了,但是实际文件并没有被删除?debug了半天才找到问题原因:原来在Linux平台上即使文件被其他程序打开也同样可以使用java文件类的delete方法删除,而Windows平台却不可以。

下面是我修改后的文件夹删除类的源代码:

public class FolderDeleter...{

private static Log log = LogFactory.getLog(FolderDeleter.class);

private static java.io.File m_root;

private static ArrayList m_dirs;

/** *//**

* 递归删除文件夹以及子文件夹

* @param dir 文件夹绝对路径

* @throws Exception 删除文件异常

*/

@SuppressWarnings("unchecked")

public static void deleteDirs(java.io.File dir) throws Exception ...{

m_root = dir;

m_dirs = new ArrayList();

if (!m_root.isDirectory()) ...{

throw new Exception("Exception:"" + m_root.toString()

+ "" is not a director");

} else ...{

// delete all director

try ...{

m_dirs.add(m_root);

myDelete();

} catch (Exception e) ...{

log.error(">>>删除文件出错:", e);

throw e;

}

}

}

/** *//**

*

* @param dirPath

*            String a director file path;

* @throws Exception

*             if dirPath is not a director file path

*/

public static void deleteDirs(String dirPath) throws Exception ...{

m_root = new File(dirPath);

deleteDirs(m_root);

}

/** *//**

* 递归删除文件夹以及子文件夹

* @param dirPath 文件夹绝对路径

* @throws Exception 删除文件异常

*/

public static void deleteSubDirs(String dirPath) throws Exception ...{

m_root = new java.io.File(dirPath);

deleteSubDirs(m_root);

}

/** *//**

* 递归删除文件夹以及子文件夹

* @param dir 文件夹绝对路径

* @throws Exception 删除文件异常

*/

public static void deleteSubDirs(java.io.File dir) throws Exception ...{

m_root = dir;

m_dirs = new ArrayList();

// deleteDirs(m_root);

if (!m_root.isDirectory()) ...{

throw new Exception("Exception:"" + m_root.toString()

+ "" is not a director");

} else ...{

for (int i = 0; i < m_dirs.size(); i++) ...{

System.out.println(((File) m_dirs.get(i)).toString());

}

try ...{

myDelete();

} catch (Exception e) ...{

e.printStackTrace();

}

}

}

/**//*

* visit all a director and save them in a list

*/

@SuppressWarnings("unchecked")

private static void visitAll(java.io.File tempRoot) throws Exception ...{

java.io.File[] dirs = tempRoot.listFiles();

if (dirs != null) ...{

List dirsList = Arrays.asList(dirs);

if (dirsList == null) ...{

try ...{

if(!tempRoot.delete())

throw new Exception("Exception: delete file " +

tempRoot.getName() + " false!");

} catch (Exception e) ...{

throw e;

}

} else ...{

m_dirs.addAll(dirsList);

for (int i = 0; i < dirsList.size(); i++) ...{

tempRoot = (java.io.File) dirsList.get(i);

visitAll(tempRoot);

}

}

}

}

/**//*

* do delete

*/

private static void myDelete() throws Exception ...{

visitAll(m_root);

if (m_dirs != null) ...{

for (int i = m_dirs.size() - 1; i >= 0; i--) ...{

java.io.File f = (java.io.File) m_dirs.remove(i);

String fileName = f.toString();

if (!f.delete()) ...{

throw new Exception("Exception: delete file " + fileName + " false!");

}

}

} else ...{

throw new Exception("Exception: read file list of " + m_root.toString()

+ "false!");

}

}

}

这个类参考了Java删除文件夹 (我参考的不是这篇文件,时间长了找不到原始文章,但是这篇文章类似)。

我在这里想说两个问题:0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值