有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

找不到Java文件路径

我正在使用Java8,Spring在Wildfly服务器上运行

我有以下包裹:

enter image description here

LanguageChunkerServiceImpl中,我试图获取en-parser-chunking.bin的句柄,但我得到一个错误:

java.io.FileNotFoundException: en-parser-chunking.bin (The system cannot find the file specified)

我的代码:

LanguageChunkerServiceImpl。java

new FileInputStream("en-parser-chunking.bin");

new FileInputStream("./src/main/java/com/jobs/spring/service/lang/en-parser-chunking.bin");

当我从main方法运行此操作时,以下操作确实有效:

new FileInputStream("./src/main/java/com/jobs/spring/service/lang/en-parser-chunking.bin");

有人能告诉我应该走哪条路吗

多谢各位


共 (2) 个答案

  1. # 1 楼答案

    如果您使用spring,您应该将文件放在资源文件夹中,而不是src/java中

  2. # 2 楼答案

    following工程:

    如果您使用的是Spring,请将该文件放入resources目录

            ClassLoader classLoader = getClass().getClassLoader();
            File file = new File(classLoader.getResource("en-parser-chunking.bin").getFile());
            System.out.println(file.getAbsolutePath());
            modelInParse = new FileInputStream(file.getAbsolutePath());