很多框架,比如springmvc,mybatis等使用注解,为了处理注解,必然要对包进行扫描,如果自己使用注解写通用代码,也要扫描包,下面代码给出扫描包的实现:
1 package com.jjh.common; 2 3 4 import java.io.BufferedInputStream; 5 import java.io.ByteArrayOutputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.net.URL; 9 import java.net.URLDecoder; 10 import java.nio.file.FileSystem; 11 import java.nio.file.FileVisitResult; 12 import java.nio.file.Path; 13 import java.nio.file.Paths; 14 import java.nio.file.SimpleFileVisitor; 15 import java.nio.file.attribute.BasicFileAttributes; 16 import java.nio.file.spi.FileSystemProvider; 17 import java.util.ArrayList; 18 import java.util.HashMap; 19 import java.util.List; 20 import java.util.Objects; 21 22 /** 23 * @author Administrator 24 * 25 */ 26 public final class Files { 27 /** 28 * 29 */ 30 private Files() { 31 // TODO Auto-generated constructor stub 32 } 33 34 static public byte[] read(InputStream in) throws IOException 35 { 36 byte[] temp=new byte[4096]; 37 ByteArrayOutputStream buffer=new ByteArrayOutputStream(); 38 BufferedInputStream bin=new BufferedInputStream(in); 39 for(;;) 40 { 41 int len=bin.read(temp); 42 if(len>-1) 43 buffer.write(temp, 0, len); 44 else 45 break; 46 } 47 return buffer.toByteArray(); 48 } 49 50 51 static public String[] scanPackage(String packageName) throws Exception { 52 packageName=packageName.replaceAll("\\.","/"); 53 URL url = Thread.currentThread().getContextClassLoader().getResource(packageName); 54 url=new URL(URLDecoder.decode(url.toString(), "UTF-8")); 55 FileSystemProvider provider =null; 56 if(url.getProtocol().equals("jar")) 57 { 58 provider = getZipFSProvider(); 59 if (provider != null) { 60 try (FileSystem fs = provider.newFileSystem(Paths.get(url.getPath().replaceFirst("file:/", "").replaceFirst("!.*", "")),new HashMap<>())) { 61 return walkFileTree(fs.getPath(packageName),null).toArray(new String[0]); 62 } catch (Exception e) { 63 throw e; 64 } 65 } 66 } 67 else if(url.getProtocol().equals("file")) 68 { 69 int end=url.getPath().lastIndexOf(packageName); 70 String basePath=url.getPath().substring(1, end); 71 return walkFileTree(Paths.get(url.getPath().replaceFirst("/","")),Paths.get(basePath)).toArray(new String[0]); 72 } 73 return null; 74 } 75 76 private static List<String> walkFileTree(Path path,Path basePath) throws IOException{ 77 // TODO Auto-generated method stub 78 final List<String> result=new ArrayList<>(); 79 java.nio.file.Files.walkFileTree(path, new SimpleFileVisitor<Path>(){ 80 private String packageName=Objects.isNull(basePath)?"":basePath.toString(); 81 /* (non-Javadoc) 82 * @see java.nio.file.SimpleFileVisitor#visitFile(java.lang.Object, java.nio.file.attribute.BasicFileAttributes) 83 */ 84 @Override 85 public FileVisitResult visitFile(Path arg0, BasicFileAttributes arg1) throws IOException { 86 // TODO Auto-generated method stub 87 if(arg0.toString().endsWith(".class")) 88 { 89 result.add(arg0.toString().replace(packageName, "").substring(1).replace("\\", "/").replace(".class", "").replace("/", ".")); 90 } 91 return FileVisitResult.CONTINUE; 92 } 93 94 /* (non-Javadoc) 95 * @see java.nio.file.SimpleFileVisitor#preVisitDirectory(java.lang.Object, java.nio.file.attribute.BasicFileAttributes) 96 */ 97 @Override 98 public FileVisitResult preVisitDirectory(Path arg0, BasicFileAttributes arg1) throws IOException { 99 // TODO Auto-generated method stub 100 //packageName=basePath==null?arg0.toString().substring(1, arg0.toString().length()-1).replace("/", "."):basePath.relativize(arg0).toString().replace("\\", "."); 101 return FileVisitResult.CONTINUE; 102 } 103 104 }); 105 return result; 106 } 107 108 static public FileSystemProvider getZipFSProvider() { 109 for (FileSystemProvider provider : FileSystemProvider.installedProviders()) { 110 if ("jar".equals(provider.getScheme())) 111 return provider; 112 } 113 return null; 114 } 115 116 public static void main(String[] args) throws Exception { 117 //C:\Program Files\Apache Software Foundation\Tomcat 8.0\wtpwebapps\j2ee\WEB-INF\hawk.xml 118 // System.out.println(new 119 // URL("jar:file:/home/whf/foo.jar!/cn/fh").getPath()); 120 for(String c:scanPackage("com.mysql.jdbc")) 121 System.out.println(c); 122 //System.out.println(Paths.get("file://C:/Users/Administrator/Desktop/pecker/build/classes/com/jjh/common")); 123 124 } 125 126 } 127