티스토리 뷰

공부

[Java] read file in jar

승가비 2020. 7. 13. 23:48
728x90
# /src/main/resources/file
FileUtils.streamToFile(getClass().getClassLoader().getResourceAsStream("file"))


public static File streamToFile(InputStream in) {
    if (in == null) {
        return null;
    }

    try {
        File f = File.createTempFile(String.valueOf(in.hashCode()), ".tmp");
        f.deleteOnExit();

        FileOutputStream out = new FileOutputStream(f);
        byte[] buffer = new byte[1024];

        int bytesRead;
        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }

        return f;
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
        return null;
    }
}

https://stackoverflow.com/questions/14089146/file-loading-by-getclass-getresource

 

File loading by getClass().getResource()

I have followed the way of loading the resource file by using getClass.getResource(path). The snippet of code is here : String url = "Test.properties"; System.out.println("Before printing paths.....

stackoverflow.com

https://stackoverflow.com/questions/941754/how-to-get-a-path-to-a-resource-in-a-java-jar-file

 

How to get a path to a resource in a Java JAR file

I am trying to get a path to a Resource but I have had no luck. This works (both in IDE and with the JAR) but this way I can't get a path to a file, only the file contents: ClassLoader classLoa...

stackoverflow.com

https://cornswrold.tistory.com/68

 

[JAVA] Jar안에 있는 resource 파일 읽기(classPath)

Jar안에 있는 resource파일 읽기 내가 만든 코드 기능 중에 classpath안에 있는 resource를 읽어들이는 기능이 있었다.(server.config.json을 읽는다.) 그런데 그 프로젝트를 Jar(Executable jar) 로 묶은 후 실행..

cornswrold.tistory.com

https://mkyong.com/java/java-read-a-file-from-resources-folder/

 

Java - Read a file from resources folder - Mkyong.com

- Java - Read a file from resources folder

mkyong.com

https://stackoverflow.com/questions/941754/how-to-get-a-path-to-a-resource-in-a-java-jar-file/62878468#62878468

 

How to get a path to a resource in a Java JAR file

I am trying to get a path to a Resource but I have had no luck. This works (both in IDE and with the JAR) but this way I can't get a path to a file, only the file contents: ClassLoader classLoa...

stackoverflow.com

 

728x90
댓글