简体   繁体   English

从源代码动态访问src / main / resources /

[英]Access src/main/resources/ dynamically from source code

My files are located under projectRoot/src/main/resources/levels/ 我的文件位于projectRoot/src/main/resources/levels/

在此处输入图片说明

If I call Utils.getFileNamesInDirectory("src/main/resources/levels") , it works. 如果我调用Utils.getFileNamesInDirectory("src/main/resources/levels") ,它将起作用。
But when project is packaged, levels directory is placed under root of .jar . 但是当打包项目时,将levels目录放置在.jar root目录下。
How can I made this dynamic in static class? 如何在静态课程中使它动态化? ie src/main/resources/ src/main/resources/
So that code will run within eclipse and as standalone jar. 因此该代码将在Eclipse中作为独立的jar运行。

Code to list files in directory .. 列出目录中文件的代码

public class Utils {
    public static List<String> getFileNamesInDirectory(String directory){

        List<String> results = new ArrayList<String>();

        File[] files = new File(directory).listFiles(new FilenameFilter() {
            public boolean accept(File dir, String filename) {
                return filename.endsWith(".json");
            }
        });

        for (File file : files) {
            if (file.isFile()) {
                results.add(file.getName());
            }
        }

        Collections.sort(results);

        return results;
    }
}

Updated 更新

I've moved to using getResourceAsStream (as getResource was causing IllegalArgumentException: URI is not hierarchical ) and I'm able to list files in a directory within Eclipse . 我已经开始使用getResourceAsStream (因为getResource导致IllegalArgumentException: URI is not hierarchical ),并且能够在Eclipse的目录中列出文件。

public static List<String> getFileNamesInDirectory(String directory){

        List<String> results = new ArrayList<String>();
        InputStream in = Utils.class.getResourceAsStream("/" + directory);
        BufferedReader rdr = new BufferedReader(new InputStreamReader(in));
        String line;

        try {

            while ((line = rdr.readLine()) != null) {
                System.out.println("file: " + line);
                results.add(new File(line).getName());
            }

            rdr.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        Collections.sort(results);

        return results;
    }

But when I run it as standalone .jar I get the following error on this line: while ((line = rdr.readLine()) != null) { 但是当我将其作为独立.jar运行时,在此行上出现以下错误: while ((line = rdr.readLine()) != null) {
Why does it not work outside Eclipse ? 为什么它在Eclipse之外不起作用?

Exception in thread "main" java.lang.NullPointerException
    at java.io.FilterInputStream.read(FilterInputStream.java:133)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:322)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:364)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:210)
    at java.io.InputStreamReader.read(InputStreamReader.java:205)
    at java.io.BufferedReader.fill(BufferedReader.java:165)
    at java.io.BufferedReader.readLine(BufferedReader.java:328)
    at java.io.BufferedReader.readLine(BufferedReader.java:393)
    at com.app.tools.Utils.getFileNamesInDirectory(Utils.java:31)

The reason your code works in Eclipse is that Eclipse launches the java process from the project directory and we can assume the path provided in 您的代码在Eclipse中工作的原因是Eclipse从项目目录启动了java进程,我们可以假定在

Utils.getFileNamesInDirectory("src/main/resources/levels")

is relative to the current working directory (the project directory). 相对于当前工作目录(项目目录)。 Since the file system location <project-directory>/src/main/resources/levels exists, it can be found and returned to you. 由于文件系统位置<project-directory>/src/main/resources/levels存在,因此可以找到它并将其返回给您。

src/main/resources is a Maven convention meant to hold resources that will eventually end up in the classpath when the project is compiled/built/deployed. src/main/resources是一种Maven约定,用于保存在编译/构建/部署项目时最终会在类路径中结束的资源。 To retrieve resources from the classpath you use Class#getResource(String) , ClassLoader#getResource(String) and/or ClassLoader#getSystemResource(String) . 要从类路径检索资源,请使用Class#getResource(String)ClassLoader#getResource(String)和/或ClassLoader#getSystemResource(String)

Now, although there are ways to list resources in the classpath , you should not typically do this. 现在, 尽管有一些方法可以在类路径中列出资源,但是通常不应该这样做。 If you need a resource, you know it by name and can therefore use one of the methods listed above to get it. 如果您需要资源,则可以按名称知道它,因此可以使用上面列出的方法之一来获取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM