2012-01-24 106 views
1

里面我有一组文件在我的Maven的资源文件夹:Maven的:遍历文件的目录Maven的资源目录

+ src 
    + main 
    + resources 
     + mydir 
     + myfile1.txt 
     + myfile2.txt 

我如何可以循环MYDIR?不仅在Eclipse中,而且在从命令行和从属jar中运行JUnit测试时。

File mydir = new File("mydir"); 
for (File f : dir.listFiles()) { 
    dosomething...  
} 

谢谢你的提示!

+0

只使用Java文件API。通过使用isDirectory()方法检查选定文件是否是目录。 – user47900

回答

0

最后,这是我想出了处理引用罐子内访问文件:

public class ResourceHelper { 

    public static File getFile(String resourceOrFile) 
     throws FileNotFoundException { 
    try { 

     // jar:file:/home/.../blue.jar!/path/to/file.xml 
     URI uri = getURL(resourceOrFile).toURI(); 
     String uriStr = uri.toString(); 
     if (uriStr.startsWith("jar")) { 

     if (uriStr.endsWith("/")) { 
      throw new UnsupportedOperationException(
       "cannot unjar directories, only files"); 
     } 

     String jarPath = uriStr.substring(4, uriStr.indexOf("!")) 
      .replace("file:", ""); 
     String filePath = uriStr.substring(uriStr.indexOf("!") + 2); 

     JarFile jarFile = new JarFile(jarPath); 
     assert (jarFile.size() > 0) : "no jarFile at " + jarPath; 

     Enumeration<JarEntry> entries = jarFile.entries(); 

     while (entries.hasMoreElements()) { 

      JarEntry jarEntry = entries.nextElement(); 
      if (jarEntry.toString().equals(filePath)) { 
      InputStream input = jarFile.getInputStream(jarEntry); 
      assert (input != null) : "empty is for " + jarEntry; 
      return tmpFileFromStream(input, filePath); 
      } 
     } 
     assert (false) : "file" + filePath + " not found in " + jarPath; 
     return null; 
     } else { 
     return new File(uri); 
     } 

    } catch (URISyntaxException e) { 
     throw new FileNotFoundException(resourceOrFile); 
    } catch (IOException e) { 
     throw new FileNotFoundException(resourceOrFile); 
    } 
    } 

    private static File tmpFileFromStream(InputStream is, String filePath) 
     throws IOException { 

    String fileName = filePath.substring(filePath.lastIndexOf("/") + 1, 
     filePath.lastIndexOf(".")); 
    assert (fileName != null) : "filename cannot be null for " + filePath; 
    String extension = filePath.substring(filePath.lastIndexOf(".")); 
    assert (extension != null) : "extension cannot be null for " + filePath; 

    File tmpFile = File.createTempFile(fileName, extension); 
    // tempFile.deleteOnExit(); 
    assert (tmpFile.exists()) : "could not create tempfile"; 

    OutputStream out = new FileOutputStream(tmpFile); 
    int read = 0; 
    byte[] bytes = new byte[1024]; 
    while ((read = is.read(bytes)) != -1) { 
     out.write(bytes, 0, read); 
    } 
    is.close(); 
    out.flush(); 
    out.close(); 
    assert (tmpFile.length() > 0) : "file empty " 
     + tmpFile.getAbsolutePath(); 
    return tmpFile; 
    } 

    public static File getTempFile(String resourceOrFile) throws IOException { 

    InputStream input = getInputStream(resourceOrFile); 

    File tempFile = IOUtils.createTempDir(); 
    tempFile.deleteOnExit(); 
    FileOutputStream output = new FileOutputStream(tempFile); 

    byte[] buffer = new byte[4096]; 
    int bytesRead = input.read(buffer); 
    while (bytesRead != -1) { 
     output.write(buffer, 0, bytesRead); 
     bytesRead = input.read(buffer); 
    } 
    output.close(); 
    input.close(); 

    return tempFile; 
    } 

    public static InputStream getInputStream(String resourceOrFile) 
     throws FileNotFoundException { 

    try { 
     return getURL(resourceOrFile).openStream(); 
    } catch (Exception e) { 
     throw new FileNotFoundException(resourceOrFile); 
    } 
    } 

    public static URL getURL(String resourceOrFile) 
     throws FileNotFoundException { 

    File file = new File(resourceOrFile); 
    // System.out.println("checking file "); 
    // is file 
    if (file.exists()) { 
     // System.out.println("file exists"); 
     try { 
     return file.toURI().toURL(); 
     } catch (MalformedURLException e) { 
     throw new FileNotFoundException(resourceOrFile); 
     } 
    } 
    // is resource 
    if (!file.exists()) { 
     // System.out.println("file resource"); 
     URL url = Thread.class.getResource(resourceOrFile); 
     if (url != null) { 
     return url; 
     } 
     url = Thread.class.getResource("/" + resourceOrFile); 
     if (url != null) { 
     return url; 
     } 
    } 
    throw new FileNotFoundException(resourceOrFile); 
    } 
} 
3

果壳,大致为:

URL pathUrl = clazz.getClassLoader().getResource("mydir/"); 
if ((pathURL != null) && pathUrl.getProtocol().equals("file")) { 
    return new File(pathUrl.toURI()).list(); 
} 

测试; Groovy:在

def resourcesInDir(String dir) { 
    def ret = [] 
    pathUrl = this.class.getClassLoader().getResource(dir) 
    if ((pathUrl != null) && pathUrl.getProtocol().equals("file")) { 
     new File(pathUrl.toURI()).list().each { 
      ret << "${dir}/${it}" 
     } 
    } 
    ret 
} 

files = resourcesInDir("tmp/") 
files.each { 
    s = this.class.getResourceAsStream(it) 
    println s.text 
} 
+0

谢谢戴夫。无论何时我在Eclipse或mvn命令行中,这都可以工作,但是当我在另一个项目中使用这个maven项目时(在这种情况下,协议是“jar:”)时不起作用。最后,我最终使用JarFile API来检索文件: – Renaud