文件夹递归遍历

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值