티스토리 뷰

공부

[Java] read file in jar

seunggabi 승가비 2020. 7. 13. 23:48
# /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

 

'공부' 카테고리의 다른 글

[Python] django 1.7 migrate gets error “table already exists”  (0) 2020.07.13
[Java] HBase secure  (0) 2020.07.13
[Java] read file in jar  (0) 2020.07.13
[Jetbrains] Reformat File dialog & short cut ⌥⇧⌘L  (0) 2020.07.13
[Linux] which command  (0) 2020.07.13
[Linux] unset env variables  (0) 2020.07.13
댓글
댓글쓰기 폼