How do I read files from Jar in Java?

Asked 2 years ago, Updated 2 years ago, 88 views

I want to read the XML file located in one of the jar files included in the class path. How do I read the file included in the jar file?

java file-io jar

2022-09-22 13:09

1 Answers

If you want to read the file from the application, use the code below:

InputStream input = getClass().getResourceAsStream("/classpath/to/my/file");

The path begins with a "/", but it is not the path of the file system and represents a classpath. If the desired file exists in classpath "org.xml" and the file name is myxml.xml, the path will be like "/org/xml/myxml.xml".

InputStream is responsible for reading the contents of the file. You can also wrap it around a Reader object as needed.

I hope it helps.


2022-09-22 13:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.