当前位置: 首页 > 知识库问答 >
问题:

SpringBoot-访问资源文件夹中的文件

常乐
2023-03-14

我正在从主类访问资源文件夹中的文件

File file = new ClassPathResource("remoteUnitsIdsInOldServer.txt").getFile();

我收到了这个错误:

java.io.FileNotFoundException: class path resource [remoteUnitsIdsInOldServer.txt] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/Users/lopes/Documents/workspace-sts-3.9.0.RELEASE/telefonicaUtils/target/telefonicaUtils-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/remoteUnitsIdsInOldServer.txt

我甚至打开了jar文件,remoteUnitsIdsInOldServer.txt文件就在那里,在类内部

共有3个答案

景育
2023-03-14

通常,源代码树如下所示:

src/
  main/
    java/
      com/
        ...
    resources/
      remoteUnitsIdsInOldServer.txt

并且,使用标准的Maven/Gradle功能,将生成如下JAR:

<JAR_ROOT>/
  com/
    ...
  remoteUnitsIdsInOldServer.txt

您列出的代码应该适用于这种情况。然而,您提到了在“类内部”查看您的JAR。我不认为JAR中会有“classes”文件夹。

祝你好运

柴瀚
2023-03-14

这取决于你的要求。

考虑到你需要从资源中访问文本文件。您可以简单地使用< code>apache IOUtils和java ClassLoader

代码段(注意:IOUtils包--

    String result = "";

    ClassLoader classLoader = getClass().getClassLoader();
    try {
        result = IOUtils.toString(classLoader.getResourceAsStream("fileName"));
    } catch (IOException e) {
        e.printStackTrace();
    }

经典方式:

StringBuilder result = new StringBuilder("");

//Get file from resources folder

ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("fileName").getFile());

try (Scanner scanner = new Scanner(file)) {

    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        result.append(line).append("\n");
    }

    scanner.close();

} catch (IOException e) {
    e.printStackTrace();
}

考虑到您需要从xml、属性文件等资源中访问属性。它太简单了,只需使用spring annotation < code > @ import resource({ " class path:application-properties . XML "," class path:context . properties " })希望对你有所帮助。

须新
2023-03-14

对我来说最简单的解决方案是,

try {
   ClassPathResource classPathResource = new ClassPathResource("remoteUnitsIdsInOldServer.txt");
   byte[] data = FileCopyUtils.copyToByteArray(classPathResource.getInputStream());
   String content = new String(data, StandardCharsets.UTF_8);
} catch (Exception ex) {
  ex.printStackTrace();
}
 类似资料:
  • 我有一个资源文件夹/包在我的项目的根,我"不"想要加载某个文件。如果我想加载某个文件,我会使用class.getResourceAsStream,我会没事的!!我实际上想做的是在资源文件夹中加载一个“文件夹”,循环该文件夹中的文件,并获得每个文件的流,并在内容中读取...假设在运行时之前没有确定文件名...那我该怎么办?有没有一种方法来获得一个文件夹中的文件列表在您的jar文件?请注意,带有资源的

  • 我正在尝试从一个java项目运行一个javascript文件。目前,我正在使用Nashorn java脚本引擎运行脚本,并使用maven编译和创建jar文件。 我得到警告: 我使用的是openjdk 11版本。这会是一个很大的问题吗?如果是这样的话,我要做的事情的最佳替代方案是什么? 我得到了一个FileNotFoundException 我有一个javascript文件脚本。js和两个java文

  • 资源(Asset)代表 source 文件夹中除了文章以外的所有文件,例如图片、CSS、JS 文件等。比方说,如果你的Hexo项目中只有少量图片,那最简单的方法就是将它们放在 source/images 文件夹中。然后通过类似于 ![](/images/image.jpg) 的方法访问它们。 对于那些想要更有规律地提供图片和其他资源以及想要将他们的资源分布在各个文章上的人来说,Hexo也提供了更组

  • ServletContext接口定义了三个方法来访问当前Web应用程序的资源文件,这三个方法如下: l getResourcePaths方法 该方法返回指定Web应用程序目录中的所有子目录和文件,这些返回的目录文件不包括嵌套目录和文件。这些返回的子目录和文件都封装在该方法返回的一个Set对象中。getResourcePaths方法的定义如下: public Set getResourcePaths

  • 我正在尝试访问src/main/Resources/XYZ/view文件夹中的xsd,其中XYZ/view文件夹是由我创建的,文件夹具有我需要进行xml验证的abc.xsd。 当我每次尝试访问xsd时,结果为null, 我试过了, 1) 2) 以及我为获取资源或类加载器等而进行的更多跟踪。 最后我得到了xsd, 文件文件 = 新文件(新类路径资源(“/src/main/resources/XYZ/

  • 问题内容: 我的/ res / raw /文件夹(/res/raw/textfile.txt)中有一个资源文件,我正在尝试从我的android应用中读取该文件进行处理。 我尝试了不同的路径语法,但始终会遇到 java.io.FileNotFoundException 错误。如何访问/res/raw/textfile.txt进行处理?是 File file = new File(“ res / ra