How to Read a File from Jar in Java

Reading a resource file from within jar

Rather than trying to address the resource as a File just ask the ClassLoader to return an InputStream for the resource instead via getResourceAsStream:

try (InputStream in = getClass().getResourceAsStream("/file.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
// Use resource
}

As long as the file.txt resource is available on the classpath then this approach will work the same way regardless of whether the file.txt resource is in a classes/ directory or inside a jar.

The URI is not hierarchical occurs because the URI for a resource within a jar file is going to look something like this: file:/example.jar!/file.txt. You cannot read the entries within a jar (a zip file) like it was a plain old File.

This is explained well by the answers to:

  • How do I read a resource file from a Java jar file?
  • Java Jar file: use resource errors: URI is not hierarchical

How to read a file from a jar file?

You can't use File, since this file does not exist independently on the file system. Instead you need getResourceAsStream(), like so:

...
InputStream in = getClass().getResourceAsStream("/1.txt");
BufferedReader input = new BufferedReader(new InputStreamReader(in));
...

Java - Reading files inside jar

JAR files are really ZIP files, so just treat the file like it is a ZIP, like what @Jamie suggested.

Reading a Text File from a .jar

Try using this

ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource(yourFileName).getFile());

if doesn't work put the resources folder inside src folder

Reading a Text File From a .jar File

The Scanner class has a constructor Scanner(InputStream); so you can still use this class to read the data like you did before.

All you have to do is to read the file from the Jar, you can do this like so:

InputStream is = getClass().getResourceAsStream("champdata.txt");
Scanner read = new Scanner(is);
read.useDelimiter("%");

Where the file named champdata.txt is located at the root of your jar file (which is just a zip file, you can use any unzipper to verify where the file is being located).

Now if you want to have the same functionality while developing in your IDE, place the file in your source directory, so that when the project is being built, it is placed in your classes folder. This way, the file can be loaded as described above using getResourceAsStream()

Reading text file works in IDE but not in .jar

You need to load the file as a resource. You can use Class.getResourceAsStream or ClassLoader.getResourceAsStream; each will give return an InputStream for the resource.

Once you've got an InputStream, wrap it in an InputStreamReader (specifying the appropriate encoding) to read text from it.

If you need to sometimes read from an arbitrary file and sometimes read from a resource, it's probably best to use separate paths to either create a FileInputStream for the file or one of the methods above for a resource, then do everything else the same way after that.

Here's an example which prints each line from resources/names.txt which should be bundled in the same jar file as the code:

package example;

import java.io.*;
import java.nio.charset.*;

public class Test {
public static void main(String[] args) throws IOException {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(
Test.class.getResourceAsStream("/resources/names.txt"),
StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
}
}


Related Topics



Leave a reply



Submit