想删除本地一个项目目录,结果windows说路径太长,不能删除。于是试了试java删除。一切ok。以后一定要抓紧时间学python。

/**
* Created by rmiao on 4/21/2016.
*/
public class TestDel { public static void main(String[] args) throws IOException {
String dest = "D:\\perforce\\workspace2\\EPC";
File file = new File(dest); forceDelete(file); } private static void forceDelete(File file) throws IOException {
if (file.isDirectory()){
deleteDirectory(file);
}else{
boolean filePresent = file.exists();
if(!file.delete()){
if(!filePresent){
throw new FileNotFoundException("File does note exist:"+file);
} String message = "Unable to delete file:"+file;
throw new IOException(message); }else{
System.out.println("-"+file.getAbsolutePath());
} }
}
public static void deleteDirectory(File directory) throws IOException{
if (directory.exists()){
cleanDirectory(directory);
}
if(!directory.delete()){
String message = "Unable to delete directory "+directory+".";
throw new IOException(message);
}
} /**
* Cleans a directory without deleting it.
*
* @param directory directory to clean
* @throws IOException in case cleaning is unsuccessful
*/
public static void cleanDirectory(File directory) throws IOException {
if (!directory.exists()) {
String message = directory + " does not exist";
throw new IllegalArgumentException(message);
} if (!directory.isDirectory()) {
String message = directory + " is not a directory";
throw new IllegalArgumentException(message);
} File[] files = directory.listFiles();
if (files == null) { // null if security restricted
throw new IOException("Failed to list contents of " + directory);
} IOException exception = null;
for (File file : files) {
try {
forceDelete(file);
} catch (IOException ioe) {
exception = ioe;
}
} if (null != exception) {
throw exception;
}
} }

java删除文件夹的更多相关文章

  1. Java删除文件夹和文件

    转载自:http://blog.163.com/wu_huiqiang@126/blog/static/3718162320091022103144516/ 以前在javaeye看到过关于Java操作 ...

  2. 用java删除文件夹里的所有文件

    import java.io.File; public class Test { public static void main(String args[]){ Test t = new Test() ...

  3. 【转】Java删除文件夹和文件

    原文网址:http://kxjhlele.iteye.com/blog/323657 以前在javaeye看到过关于Java操作文件的一篇文章,写的很好,但找了半天也没找到,就把找到底几篇文章整理一下 ...

  4. java删除文件夹下所有文件

    package org.sw; import java.io.File; /** * * @author mengzw * @since 3.0 2014-2-26 */ public class D ...

  5. java删除文件夹 Java中实现复制文件或文件夹

    删除文件夹 import java.io.File; public class DeleteDir { /** * @param args */ public static void main(Str ...

  6. java 删除文件夹 / 删除某文件夹下的所有文件

    import java.io.File; /*************************删除文件夹delFolder / 删除文件夹中的所有文件delAllFile *start******** ...

  7. java 删除文件夹中的所有文件及文件夹

    删除文件夹(前提:文件夹为空以及InputStream和OutputStream等一些数据文件流关掉[close()],否则文件无法删除) //删除文件夹 public static void del ...

  8. java删除文件夹及子目录

    package test; import java.io.FileNotFoundException; import java.io.IOException; import java.io.File; ...

  9. Java删除文件夹和其子文件、文件的拷贝和剪切

     1.递归删除目录下的所有文件及子目录下所有文件 //递归删除目录下的所有文件及子目录下所有文件 public static boolean deleteDir(File dir) { if (dir ...

随机推荐

  1. js将数字转成大写中文

    <script type="text/javascript"> //主函数 function DX(n) { if (!/^(0|[1-9]\d*)(\.\d+)?$/ ...

  2. jquery/zepto 圣诞节雪花飞扬

    下载地址: http://www.html5tricks.com/jquery-html5-christ-snow.html 演示地址: http://www.html5tricks.com/jque ...

  3. winform设置文本框宽度 根据文字数量和字体返回宽度

    _LinkLabel.Width = TextRenderer.MeasureText(_LinkLabel.Text, _LinkLabel.Font).Width;

  4. ReactJS入门(一)—— 初步认识React

    React刚开始红的时候,由于对其不甚了解,觉得JSX的写法略非主流,故一直没打算将其应用在项目上,随着身边大神们的科普,才后知后觉是个好东西. 好在哪里呢?个人拙见,有俩点: 1. 虚拟DOM —— ...

  5. NoSQL初探之人人都爱Redis:(2)Redis API与常用数据类型简介

    一.Redis API For .Net 首先,不得不说Redis官方提供了众多的API开发包,但是目前Redis官方版本不支持.Net直接进行连接,需要使用一些第三方的开源类库.目前最流行的就是Se ...

  6. 谁能在同一文件序列化多个对象并随机读写(反序列化)?BinaryFormatter、SoapFormatter、XmlSerializer还是BinaryReader

    谁能在同一文件序列化多个对象并随机读写(反序列化)?BinaryFormatter.SoapFormatter.XmlSerializer还是BinaryReader 随机反序列化器 +BIT祝威+悄 ...

  7. 一个不错的vue表单验证插件

    github文档 用着不错,官方的文档例子很简单 <body> <div id="app"> <validator name="valida ...

  8. android 获取屏幕宽度和高度

    // 获取屏幕宽高(方法1) int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // 屏幕宽(像素,如:480p ...

  9. rem与px的转换

    rem与px的转换 引用自http://caibaojian.com/rem-and-px.html A-A+ 前端博客•前端开发教程•rem•3702View0 rem是相对于根元素<html ...

  10. PHP面向对象笔记

    一.构造函数.析构函数(1)构造函数:__construct()说明:对象被实例化时调用,可带参数例: $obj = new A($a,$b); (2)析构函数:_destruct()说明:页面执行结 ...