Java 目录递归遍历

  1 package cn.sytz.picturePicker;
  2 
  3 import java.io.File;
  4 import java.io.IOException;
  5 import java.util.ArrayList;
  6 
  7 import javax.servlet.http.HttpServletRequest;
  8 import javax.servlet.http.HttpServletResponse;
  9 
 10 import org.apache.struts.action.ActionForm;
 11 import org.apache.struts.action.ActionMapping;
 12 import org.apache.struts.actions.DispatchAction;
 13 
 14 /*
 15  * 设想: surveilPictureBean - 监控图片 Bean
 16  * bean.sourceRootPath String // 源根目录
 17  * bean.targetRootPath String // 目标根目录
 18  * bean.sourceAbsolutePath String // 源绝对路径
 19  * bean.relativePath String // 相对路径,含文件名
 20  * bean.relativePathWithoutFileName String // 相对路径,不含文件名
 21  * bean.pictureFileName // 图片文件名
 22  * 
 23  */
 24 
 25 public class DirectoryRecursion extends DispatchAction {
 26     
 27     // private static ArrayList filelist = new ArrayList(); 
 28     
 29     // 根目录,只处理其下子目录及文件
 30     private static String sourceRootPath = "F:\\资料\\电子警察照片\\复件 122 - 副本" ;
 31     private static String targetRootPath = "D:\\目标文件夹" ;
 32     
 33     /*
 34      * 构造函数
 35      */
 36     public DirectoryRecursion(){
 37         // this.main(null);
 38     }
 39     
 40     /*
 41      * 主函数
 42      */
 43     public static void main(ActionMapping mapping, ActionForm form,
 44             HttpServletRequest request, HttpServletResponse response)
 45             throws Exception {
 46         
 47         
 48 
 49         try{
 50 
 51             long a = System.currentTimeMillis();
 52             
 53             // refreshFileList("c:\\java");
 54             refreshFileList( sourceRootPath );
 55             
 56             System.out.println( System.currentTimeMillis() - a);
 57 
 58         } catch ( Exception e ) {
 59             
 60             System.out.println( " e.getMessage(): " + e.getMessage() ); // 输出跟踪信息
 61             e.printStackTrace();
 62         }
 63         
 64         
 65     }
 66     
 67     /*
 68      * 刷新文件列表
 69      */
 70     public static void refreshFileList( String strPath ) throws IOException { 
 71         
 72         File dir = new File( strPath ); 
 73         File[] files = dir.listFiles(); 
 74         
 75         // 相对路径
 76         String relativePath = "" ;
 77         
 78         // 空目录
 79         if ( files == null ) return ;
 80         
 81         // 遍历
 82         for ( int i = 0; i < files.length; i++ ) { 
 83             
 84             if ( files[i].isDirectory()) { 
 85                 // 目录
 86                 refreshFileList( files[i].getAbsolutePath() ); 
 87             } else {
 88                 
 89                 // 文件
 90                 String strFileName = files[i].getAbsolutePath().toLowerCase(); 
 91                 System.out.println("文件名:" + strFileName );
 92                 
 93                 // 相对路径,含文件名
 94                 relativePath = strFileName.substring( sourceRootPath.length() );
 95                 System.out.println("relativePath:" + relativePath );
 96                 
 97                 // System.out.println("文件名:" + files[i].getPath() );
 98                 // System.out.println("文件名:" + files[i].getCanonicalPath() );
 99                 
100                 // 仅目录,不含文件名
101                 System.out.println("仅目录:" + files[i].getParent() ); 
102                 
103                 // 仅目录,相对路径,不含文件名
104                 System.out.println("仅目录(相对路径):" + files[i].getParent().substring( sourceRootPath.length() ) ); 
105                 
106                 // 仅文件名,不含路径
107                 System.out.println("仅文件名:" + files[i].getName() ); 
108                 
109                 // 目标文件夹 targetPath
110                 File targetPath = new File( targetRootPath + files[i].getParent().substring( sourceRootPath.length() ) );
111 
112                 // 如果目标文件夹不空 并且 不存在 则创建。
113                 if( targetPath != null && ! targetPath.exists() ){ 
114                     targetPath.mkdirs(); 
115                 } 
116                 
117                 // filelist.add( files[i].getAbsolutePath() );                    
118             } 
119         } 
120     }
121 }

 

posted on 2013-05-30 14:29  Livon  阅读(558)  评论(0编辑  收藏  举报

导航