删除指定文件夹以及文件夹中的文件

版权声明:原创不易,转载请注明出处~ https://blog.csdn.net/qq_34266804/article/details/87928957

删除文件夹,文件夹的路径我是写死的绝对路径,注意系统中的路径为:

D:\tomcat\apache-tomcat-7.0.90\webapps\zsmh\upload

但是你通过java获取过来的路径却是:D:/tomcat/apache-tomcat-7.0.90/webapps/zsmh/upload    

因为“\”是转义字符。

你只需要将获取过来的路径中的“\”全部替换为“/”,即可。

String 	fullFilePath1 = "D:\MyEclipse\apache-tomcat-8.5.37\webapps\zsmh\upload\";
	boolean flag = false;
					//删除文件夹以及所有文件start
					File file1 = new File(fullFilePath1);
					if (!file1.exists()) {
//					    return flag;
					}
					if (!file1.isDirectory()) {
//					    return flag;
					}
					String[] tempList = file1.list();
					File temp = null;
					for (int i = 0; i < tempList.length; i++) {
					    if (fullFilePath1.endsWith(File.separator)) {
						temp = new File(fullFilePath1 + tempList[i]);
					    } else {
						temp = new File(fullFilePath1 + File.separator + tempList[i]);
					    }
					    if (temp.isFile()) {
						temp.delete();
					    }
					    if (temp.isDirectory()) {
						delAllFile(fullFilePath1 + "/" + tempList[i]);// 先删除文件夹里面的文件
						delFolder(fullFilePath1 + "/" + tempList[i]);// 再删除空文件夹
						flag = true;
					    }
					}
			
					//删除文件夹end

猜你喜欢

转载自blog.csdn.net/qq_34266804/article/details/87928957
今日推荐