Java 如何以编程方式获取资源目录路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19414453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:57:20  来源:igfitidea点击:

How to get resources directory path programmatically

javaspringio

提问by whyem

I have the following directory layout:

我有以下目录布局:

  • src
    • main
      • java
      • resources
        • sql (scripts for database)
        • spring (configuration)
      • webapp
  • 源文件
    • 主要的
      • 爪哇
      • 资源
        • sql(数据库脚本)
        • 弹簧(配置)
      • 网络应用程序

Within a ServletContextListener class, I want to access the files under the SQL directory and list them. Basically my problem is with the path, because I know that listing files under a directory in a nutshell is:

在 ServletContextListener 类中,我想访问 SQL 目录下的文件并列出它们。基本上我的问题出在路径上,因为我知道在一个目录下列出文件简而言之是:

File folder = new File(path);
File[] listOfFiles = folder.listFiles();

Maybe I could use the ServletContextEventObject to try and build a path to resources/sql

也许我可以使用ServletContextEvent对象来尝试构建一条路径resources/sql

public void contextInitialized(ServletContextEvent event) {
    event.getServletContext(); //(getRealPath etc.)
}

Does something exist to set that path in a relative, non-hardcoded way? Something like new File("classpath:sql")(preferably spring if possible) or what should I do with the servletContext to point at resources/sql?

是否存在以相对的非硬编码方式设置该路径的方法?像new File("classpath:sql")(如果可能的话最好是弹簧)或者我应该如何处理 servletContext 指向resources/sql

采纳答案by Dev

I'm assuming the contents of src/main/resources/is copied to WEB-INF/classes/inside your .war at build time. If that is the case you can just do (substituting real values for the classname and the path being loaded).

我假设在构建时将内容src/main/resources/复制到WEB-INF/classes/您的 .war 中。如果是这种情况,您可以这样做(用实际值替换类名和正在加载的路径)。

URL sqlScriptUrl = MyServletContextListener.class
                       .getClassLoader().getResource("sql/script.sql");

回答by samlewis

import org.springframework.core.io.ClassPathResource;

...

File folder = new ClassPathResource("sql").getFile();
File[] listOfFiles = folder.listFiles();

It is worth noting that this will limit your deployment options, ClassPathResource.getFile()only works if the container has exploded (unzipped) your war file.

值得注意的是,这将限制您的部署选项,ClassPathResource.getFile()仅当容器已分解(解压缩)您的 war 文件时才有效。

回答by whyem

Finally, this is what I did:

最后,这就是我所做的:

private File getFileFromURL() {
    URL url = this.getClass().getClassLoader().getResource("/sql");
    File file = null;
    try {
        file = new File(url.toURI());
    } catch (URISyntaxException e) {
        file = new File(url.getPath());
    } finally {
        return file;
    }
}

...

...

File folder = getFileFromURL();
File[] listOfFiles = folder.listFiles();

回答by grep

Just use com.google.common.io.Resourcesclass. Example:

只需使用com.google.common.io.Resources类。例子:

 URL url = Resources.getResource("file name")

After that you have methods like: .getContent(), .getFile(), .getPath() etc

之后你有像这样的方法:.getContent(), .getFile(), .getPath() 等