列出java项目中resources目录中的所有文件

时间:2016-03-07 17:12:08

标签: java spring io

我想列出资源类路径中的所有文件,我使用了以下代码但在fList中获取了null异常

enter image description here

 String path = request.getSession().getServletContext().getRealPath("/resources/rules");
    File directory = new File(path);
    File[] fList = directory.listFiles();
    for (File file : fList){
        if (file.isFile()){
            System.out.println(file.getName());
        }
    }

2 个答案:

答案 0 :(得分:0)

这是一个有效的例子:

https://github.com/bleujin/aradon/blob/master/src/net/ion/nradon/helpers/ClassloaderResourceHelper.java#L30

public static Iterable<FileEntry> listFilesRelativeToClass(Class<?> clazz, String subdirectory) throws IOException {
    ArrayList<FileEntry> list = new ArrayList<FileEntry>();
    CodeSource src = clazz.getProtectionDomain().getCodeSource();
    if (src == null) {
        return list;
    }
    URL classpathEntry = src.getLocation();
    try {
        // Check if we're loaded from a folder
        File file = new File(new File(classpathEntry.toURI()), subdirectory);
        if (file.isDirectory()) {
            return fileEntriesFor(file.listFiles());
        }
    } catch (URISyntaxException e) {
        // Should never happen, because we know classpathentry is valid
        throw new RuntimeException(e);
    }

    // We're not in a folder, so we must be in a jar or similar
    subdirectory = subdirectory.replace(File.separatorChar, '/');
    if (!subdirectory.endsWith("/")) {
        subdirectory = subdirectory + "/";
    }

    ZipInputStream jarStream = new ZipInputStream(classpathEntry.openStream());
    ZipEntry zipEntry;
    while ((zipEntry = jarStream.getNextEntry()) != null) {
        if (isChild(subdirectory, zipEntry.getName())) {
            String basename = zipEntry.getName().substring(subdirectory.length());
            int indexOfSlash = basename.indexOf('/');
            if (indexOfSlash < 0 || indexOfSlash == basename.length() - 1) {
                list.add(new FileEntry(basename));
            }
        }
    }
    return list;
}

private static boolean isChild(String parent, String name) {
    return name.startsWith(parent);
}

public static Iterable<FileEntry> fileEntriesFor(File[] files) {
    List<FileEntry> fileEntries = new ArrayList<FileEntry>(files.length);
    for (File file : files) {
        String filename = file.getName();
        if (file.isDirectory()) {
            filename += "/";
        }
        fileEntries.add(new FileEntry(filename));
    }
    return fileEntries;
}

}

答案 1 :(得分:0)

这个源代码怎么样?

 String[] resoures = srcDir.list( new java.io.FilenameFilter()
        {
            public boolean accept( File dir, String name )
            {
                String[] extensions = { ".png", ".jar", ".txt" };
                String fileName = name.toLowerCase( Locale.getDefault() );
                for ( int i = 0; i < extensions.length; i++ )
                {
                    if ( fileName.endsWith( extensions[i] ) )
                    {
                        return true;
                    }
                }
                return false;

            }
        } );

完整来源:

http://code.openhub.net/file?fid=kpl9VrAT-CHXE496rmMP8Jbzo5U&cid=y9kXCqEmUoY&s=List%20all%20files%20in%20resoures%20directory%20in%20java%20project&pp=0&fl=Java&ff=1&projSelected=false&filterChecked,=true&mp,=1&mp=1&ml=1&me=1&md=1#L64