文件夹递归遍历

来源:互联网 发布:mac关闭页面快捷键 编辑:程序博客网 时间:2024/06/06 08:34
public class FileUtils {public static void listDirectory(File file) {//递归遍历文件夹及其子目录.if(!file.exists()){System.out.println("此路径不存在");}if(!file.isDirectory()){System.out.println("此路径不是文件夹.");}File[] files=file.listFiles();if(files!=null&&files.length>0){//说明此目录下存在文件或目录.for (File file2 : files) {if(file2.isDirectory()){listDirectory(file2);}else{System.out.println(file2.getAbsolutePath());}}}}}-----------------------------------------------------------------------------------------------------------------File file=new File("C:\\Users\\Administrator\\Desktop\\1.txt");if(!file.exists()){try {file.createNewFile();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}else{System.out.println(file.getName());file.delete();}//file.createNewFile():创建此抽象路径所表示的文件.(Atomically creates a new, empty file named by this abstract pathname if * and only if a file with this name does not yet exist.)//file.mkdir():创建此抽象路径所表示的一级目录.//file.mkdirs():创建此抽象路径所表示的多级目录.(* Creates the directory named by this abstract pathname, including any * necessary but nonexistent parent directories. Note that if this * operation fails it may have succeeded in creating some of the necessary * parent directories.)//file.delete():删除此抽象路径所表示的文件;或是此抽象路径所表示的目录(前提是这个目录必须为空,才能删除成功).//file.separator 或者用转义字符"\\"表示分隔符.//file.getName():返回该文件或目录的名字(包含文件扩展名).//file.getAbsolutelyPath():返回该文件或目录的绝对路径.//file.getParent():返回该文件或者目录的父目录的路径(* Returns the pathname string of this abstract pathname's parent, or * null if this pathname does not name a parent directory.).//file.listFiles():返回该目录下所有目录和文件的抽象路径对象所组成的数组.(* Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.)//file.list():返回该目录下所有目录和文件的名字(String Array.)(* Returns an array of strings naming the files and directories in the * directory denoted by this abstract pathname.)//file.exist():判断此抽象路径所表示文件或目录是否存在.(* Tests whether the file or directory denoted by this abstract pathname * exists.)
0 0
原创粉丝点击