How to reference a resource file correctly for JAR and Debugging?

前端 未结 7 462
不思量自难忘°
不思量自难忘° 2020-12-02 21:58

I have a nasty problem referencing resources when using a Maven project and Jar files...

I have all my resources in a dedicated folder /src/main/resources which is p

7条回答
  •  隐瞒了意图╮
    2020-12-02 22:39

    Just copy the file to a temporary directory.

    String tempDir = System.getProperty("java.io.tmpdir");
    File file = new File(tempDir.getAbsolutePath(), "filename.txt");
    if (!file.exists()) {
         InputStream is = (getClass().getResourceAsStream("/filename.txt"));
         Files.copy(is, file.getAbsoluteFile().toPath());
    }
    

提交回复
热议问题