简体   繁体   English

从子项目Spring的资源文件夹中读取txt文件

[英]Read txt file from resources folder in sub project Spring

Am trying to read a file from a subproject sample-dfx-sbi 我正在尝试从子项目sample-dfx-sbi中读取文件

file is located inside src/main/resources folder 文件位于src / main / resources文件夹内

exact file location is src/main/resources/wm/device.txt 确切的文件位置是src / main / resources / wm / device.txt

Am using below logic to read file 我正在使用以下逻辑读取文件

static final ClassLoader loader = DummyClass.class.getClassLoader();

public void getData(String ip)throws IOException {

    String filepath = loader.getResource("/wm/device.txt").toString();
    System.out.println(filepath);
    File file = new File(filepath);

    FileReader fileReader = new FileReader(file);  // exception

    ...

Exception: 例外:

jar:file:/home/user/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/sample-dfx-web/WEB-INF/lib/sample-dfx-sbi-0.0.1-SNAPSHOT.jar!/wm/device.txt

java.io.FileNotFoundException: jar:file:/home/user/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/sample-dfx-web/WEB-INF/lib/sample-dfx-sbi-0.0.1-SNAPSHOT.jar!/wm/device.txt (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)

When I go to above location inside workspace and extract jar file I can see wm/device.txt 当我转到工作空间内的上述位置并提取jar文件时,我可以看到wm / device.txt

Then why is file not loading ? 那为什么不加载文件呢?

Read resource as stream when you are binding it in JAR . JAR中绑定资源时,将其作为流读取。

public String loadResource(String filelocation) {
    if (filelocation != null && !filelocation.trim().isEmpty()) {
        try (
            InputStream inputStream = getClass().getResourceAsStream(filelocation);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));) {

            String fileLine = "";
            StringBuffer stringBuffer = new StringBuffer();

            while((fileLine = bufferedReader.readLine())!= null){
                stringBuffer.append(fileLine);
            }
            return stringBuffer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return null;
}

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

相关问题 从 Docker 容器中的 maven Quarkus 项目的资源文件夹中读取 txt 文件 - Read txt file from resources folder on maven Quarkus project From Docker Container 在 Spring Boot 中从 resources 文件夹中读取文件 - Read file from resources folder in Spring Boot 尝试从资源文件夹中读取 txt 文件时出现 Java FileNotFoundException - Java FileNotFoundException when trying to read txt file from resources folder 如何使用Java Netbeans读取项目文件夹外部的txt文件 - How to read in txt file outside of project folder with Java Netbeans Maven项目-从资源文件夹(项目根目录)外部加载文件 - Maven project - load file from outside of resources folder (project root) 如何从属性文件的资源文件夹中读取文件 - how to read a file from resources folder in properties file 如何读取资源文件夹中的文件 - How to read file in resources folder 将文件写入Spring中的resources文件夹 - Write a file to resources folder in Spring 从与 JAR 文件相同的文件夹中读取文件,但从 IDE 加载时仍读取资源文件夹 - Read a file from same folder as JAR file but still read resources folder when loading from IDE 从项目目录中的文件夹中读取文件 - Read file from a folder inside the project directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM