首页 > 编程语言 > 详细

java遍历所有目录和文件

时间:2015-03-06 17:22:47      阅读:247      评论:0      收藏:0      [点我收藏+]

package xian;


import java.io.File;

import java.util.ArrayList;


public class GetFile {

private static ArrayList<String> fileList = new ArrayList<String>();

   public static void main(String[] args) {

String filePath="E://小仙U盘";

getFiles(filePath);

}

    /**

     * 通过递归过得目录和文件

     */

   public static void getFiles(String filePath){

  File fileAll=new File(filePath);

  File[] files=fileAll.listFiles();

  for(File file:files){

  if(file.isDirectory()){//如果是目录

  //递归

  getFiles(file.getAbsolutePath());

  fileList.add(file.getAbsolutePath());

  System.out.println("显示"+filePath+"下所有子目录及其文件"+file.getAbsolutePath());

  }else{

  System.out.println("显示"+filePath+"下所有子目录"+file.getAbsolutePath());

  }

  }

   }

}

显示E:\小仙U盘\extjs\ExtJs包和Api\ext-4.0.7-gpl\resources\themes\images\gray\btn下所有子目录E:\小仙U盘\extjs\ExtJs包和Api\ext-4.0.7-gpl\resources\themes\images\gray\btn\btn-default-toolbar-small-focus-bg.gif

显示E:\小仙U盘\extjs\ExtJs包和Api\ext-4.0.7-gpl\resources\themes\images\gray\btn下所有子目录E:\小仙U盘\extjs\ExtJs包和Api\ext-4.0.7-gpl\resources\themes\images\gray\btn\btn-default-toolbar-small-focus-corners.gif

显示E:\小仙U盘\extjs\ExtJs包和Api\ext-4.0.7-gpl\resources\themes\images\gray\btn下所有子目录E:\小仙U盘\extjs\ExtJs包和Api\ext-4.0.7-gpl\resources\themes\images\gray\btn\btn-default-toolbar-small-focus-sides.gif

显示E:\小仙U盘\extjs\ExtJs包和Api\ext-4.0.7-gpl\resources\themes\images\gray\btn下所有子目录E:\小仙U盘\extjs\ExtJs包和Api\ext-4.0.7-gpl\resources\themes\images\gray\btn\btn-default-toolbar-small-over-bg.gif

显示E:\小仙U盘\extjs\ExtJs包和Api\ext-4.0.7-gpl\resources\themes\images\gray\btn下所有子目录E:\小仙U盘\extjs\ExtJs包和Api\ext-4.0.7-gpl\resources\themes\images\gray\btn\btn-default-toolbar-small-over-corners.gif


本文出自 “贾小仙” 博客,请务必保留此出处http://hackerxian.blog.51cto.com/9240575/1617892

java遍历所有目录和文件

原文:http://hackerxian.blog.51cto.com/9240575/1617892

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!