标签:
文件夹下所有子文件及文件夹的显示:
package xz_file; import java.io.File; import java.io.IOException; public class exFile { public static void main(String[] args) { File file = new File("C:/Users/xz/Workspaces/MyEclipse 9/xzLearn"); listfile(file); } public static void listfile(File file) { File lf[] = file.listFiles(); for (int i = 0; i < lf.length; i++) { if (lf[i] != null) { try { if (lf[i].isFile()) { System.out.println(lf[i].getName()); } else if(lf[i].isDirectory()){ System.out.println(lf[i].getName()); listfile(lf[i]); } } catch (Exception e) { // TODO: handle exception System.out.println("文件类型出错!"); } } } } }
结果:.classpath
.project
.settings
org.eclipse.jdt.core.prefs
bin
xz_file
exFile.class
src
xz_file
exFile.java
xz
eq.txt
xz1
13.txt
xz2
123e.txt
weqwe.txt
xz3
xz4
qdawe.txt
删除文件夹下所有文件:
package xz_file; import java.io.File; import java.io.IOException; public class exFile { public static void main(String[] args) { File file = new File("C:/Users/xz/Workspaces/MyEclipse 9/xzLearn/xz"); DeleteFile(file); } public static void DeleteFile(File file) { File lf[] = file.listFiles(); for (int i = 0; i < lf.length; i++) { if(lf[i].isDirectory()&&lf[i]!=null){ DeleteFile(lf[i]); }else if(lf[i].isFile()){ lf[i].delete(); }else{ lf[i].delete(); } } } }
结果:C:\Users\xz\Workspaces\MyEclipse 9\xzLearn\xz\xz1\xz2\xz3\xz4
只是删除了file文件,但是文件夹得以保留,所以需要修改代码。给菜狗一点时间,慢慢来。。
标签:
原文地址:http://www.cnblogs.com/xz-blogs/p/4961258.html