java小程序,用java在指定目录或以及子目录中找出同名文件,java File过滤文件名后找同名文件
主要是使用java API“java.io.File”实现
一个简单的类,copy出来,因main方法可直接运行。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.paic.icore.pams.auto.util; 2 3 import java.io.File; 4 import java.util.ArrayList; 5 import java.util.HashMap; 6 import java.util.Iterator; 7 import java.util.List; 8 import java.util.Map; 9 import java.util.Map.Entry; 10 11 /** 12 * @author White lion 13 * 14 */ 15 public class FindSameFile { 16 static int filterFilesCount=0,filterFileNamesCount=0; 17 static List<String> filterFiles = new ArrayList<String>(),filterFileNames = new ArrayList<String>(); 18 static Map<String, Object> sameFileNames = new HashMap<String, Object>(),fileNames = new HashMap<String, Object>(); 19 static File root,aRoot,bRoot; 20 21 //execue init data 22 static{ 23 filterFileNames.add("svn-"); 24 filterFileNames.add("index.html"); 25 26 filterFiles.add(".svn"); 27 filterFiles.add("image"); 28 filterFiles.add("htmlDemo"); 29 30 //目录A 31 aRoot = new File("E:/Software/src/webroot"); 32 //目录B 33 bRoot = new File("D:/Software/webroot"); 34 35 root = new File("E:/Software/src/webroot"); 36 } 37 38 //execue 39 public static void main(String[] args) { 40 long start =System.currentTimeMillis(); 41 // findFile(root, fileNames, null, null); 42 findSameFileName(aRoot,bRoot); 43 System.out.println("耗时:"+(System.currentTimeMillis()-start)+"ms,sameFileNames count:" + sameFileNames.size()+",fileNames count:" + fileNames.size()+",filterFilesCount:" + filterFilesCount+",filterFileNamesCount:" + filterFileNamesCount); 44 } 45 46 public static void findSameFileName(File aRoot, File bRoot) { 47 long start =System.currentTimeMillis(); 48 Map<String, Object> aFileNames = new HashMap<String, Object>(); 49 findFile(aRoot,aFileNames,filterFiles,filterFileNames); 50 51 Map<String, Object> bFileNames = new HashMap<String, Object>(); 52 findFile(bRoot,bFileNames,filterFiles,filterFileNames); 53 54 System.out.println(aRoot+" list:\n"+aFileNames.keySet()); 55 System.out.println(bRoot+" list:\n"+bFileNames.keySet()); 56 57 Iterator<Entry<String, Object>> iterator; 58 Map<String, Object> stayFindFileNames = new HashMap<String, Object>(); 59 if(aFileNames.size() >= bFileNames.size()){ 60 iterator=aFileNames.entrySet().iterator(); 61 stayFindFileNames=bFileNames; 62 }else{ 63 iterator=bFileNames.entrySet().iterator(); 64 stayFindFileNames=aFileNames; 65 } 66 67 while (iterator.hasNext()) { 68 Entry<String, Object> next = iterator.next(); 69 String key = next.getKey(); 70 Object value = next.getValue(); 71 72 if(null!=stayFindFileNames.get(key)){ 73 Object stayObj = stayFindFileNames.get(key); 74 75 if(value instanceof java.io.File){ 76 File a = (File) value; 77 addFileToSameFileNamesBox(stayObj, a); 78 }else if(value instanceof List){ 79 List<File> list = (List<File>) value; 80 for (File f : list) { 81 addFileToSameFileNamesBox(stayObj, f); 82 } 83 84 } 85 86 } 87 88 } 89 System.out.println("findSameFileName 耗时:"+(System.currentTimeMillis()-start)+"ms,sameFileNames count:" + sameFileNames.size()+",sameFileNames list:\n" + sameFileNames.keySet()); 90 } 91 92 93 public static void findFile(File root, Map<String, Object> fileNames,List<String> filterFiles,List<String> filterFileNames) { 94 if (root.exists() && root.isDirectory()) { 95 for (File file : root.listFiles()) { 96 if (file.isFile()) { 97 if(null!=filterFileNames){ 98 int tmpFilterFileNamesCount=filterFileNamesCount; 99 for (String filter : filterFileNames) { 100 if(file.getName().indexOf(filter)>-1){ 101 filterFileNamesCount++; 102 System.out.println("filter fileName1:"+file.getPath());//这里输出文件名! 103 break; 104 } 105 } 106 if(tmpFilterFileNamesCount<filterFileNamesCount) continue; 107 } 108 if(null!=fileNames.get(file.getName())){ 109 Object value = fileNames.get(file.getName()); 110 if(value instanceof java.io.File){ 111 File b = (File) value; 112 List<File> list = new ArrayList<File>(); 113 fileNames.put(b.getName(), list); 114 list.add(b); 115 list.add(file); 116 }else if(value instanceof List){ 117 List<File> b = (List<File>) value; 118 b.add(file); 119 } 120 }else{ 121 fileNames.put(file.getName(), file); 122 } 123 // System.out.println("find:"+file.getPath());//这里输出文件名! 124 } else if (file.isDirectory()) { 125 if(null!=filterFiles){ 126 if(filterFiles.contains(file.getName())){ 127 filterFilesCount++; 128 System.out.println("filter file2:"+file.getPath());//这里输出文件名! 129 continue; 130 } 131 } 132 findFile(file, fileNames, filterFiles, filterFileNames); 133 } 134 135 } 136 } 137 138 } 139 140 public static void addFileToSameFileNamesBox(File file) { 141 if(null!=sameFileNames.get(file.getName())){ 142 Object value = sameFileNames.get(file.getName()); 143 if(value instanceof java.io.File){ 144 File b = (File) value; 145 List<File> list = new ArrayList<File>(); 146 sameFileNames.put(b.getName(), list); 147 list.add(b); 148 list.add(file); 149 }else if(value instanceof List){ 150 List<File> b = (List<File>) value; 151 b.add(file); 152 } 153 }else{ 154 sameFileNames.put(file.getName(), file); 155 } 156 } 157 158 public static void addFileToSameFileNamesBox(Object stayObj, File file) { 159 if(stayObj instanceof java.io.File){ 160 File stayFile = (File) stayObj; 161 if(stayFile.getName().equals(file.getName())){ 162 addFileToSameFileNamesBox(stayFile); 163 } 164 }else if(stayObj instanceof List){ 165 List<File> list = (List<File>) stayObj; 166 for (File f : list) { 167 if(f.getName().equals(file.getName())){ 168 addFileToSameFileNamesBox(f); 169 } 170 } 171 } 172 } 173 174 }
作者:SVENNEE
出处:http://www.cnblogs.com/svennee
本文采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
如果您觉得此文有帮助,请点击推荐和关注我。您的支持将鼓励我继续创作!
![](https://images.cnblogs.com/cnblogs_com/svennee/655802/o_by-nc-sa_2.5_88x31.png)
出处:http://www.cnblogs.com/svennee
本文采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
如果您觉得此文有帮助,请点击推荐和关注我。您的支持将鼓励我继续创作!
![](https://images.cnblogs.com/cnblogs_com/svennee/655802/o_by-nc-sa_2.5_88x31.png)