Java.Nio.File.Path for a Classpath Resource

java.nio.file.Path for a classpath resource

This one works for me:

return Path.of(ClassLoader.getSystemResource(resourceName).toURI());

How to read text file from classpath in Java?

With the directory on the classpath, from a class loaded by the same classloader, you should be able to use either of:

// From ClassLoader, all paths are "absolute" already - there's no context
// from which they could be relative. Therefore you don't need a leading slash.
InputStream in = this.getClass().getClassLoader()
.getResourceAsStream("SomeTextFile.txt");
// From Class, the path is relative to the package of the class unless
// you include a leading slash, so if you don't want to use the current
// package, include a slash like this:
InputStream in = this.getClass().getResourceAsStream("/SomeTextFile.txt");

If those aren't working, that suggests something else is wrong.

So for example, take this code:

package dummy;

import java.io.*;

public class Test
{
public static void main(String[] args)
{
InputStream stream = Test.class.getResourceAsStream("/SomeTextFile.txt");
System.out.println(stream != null);
stream = Test.class.getClassLoader().getResourceAsStream("SomeTextFile.txt");
System.out.println(stream != null);
}
}

And this directory structure:

code
dummy
Test.class
txt
SomeTextFile.txt

And then (using the Unix path separator as I'm on a Linux box):

java -classpath code:txt dummy.Test

Results:

true
true

How to get absolute path to file in /resources folder of your project

You can use ClassLoader.getResource method to get the correct resource.

URL res = getClass().getClassLoader().getResource("abc.txt");
File file = Paths.get(res.toURI()).toFile();
String absolutePath = file.getAbsolutePath();

OR

Although this may not work all the time, a simpler solution -

You can create a File object and use getAbsolutePath method:

File file = new File("resources/abc.txt");
String absolutePath = file.getAbsolutePath();

How can I read a file from the classpath in a JAR?

You cant refer the file from jar the way you do it in from resources. Since the file is packaged inside the jar you need to read it as resource.
You have to read the file as resource using classloader.

sample code:

ClassLoader CLDR = this.getClass().getClassLoader();
InputStream inputStream = CLDR.getResourceAsStream(filePath);

If you are using java 8 and above then you can use below code using nio to read your file:

final Path path = Paths.get(Main.class.getResource(fileName).toURI());
final byte[] bytes = Files.readAllBytes(path);
String fileContent = new String(bytes, CHARSET_ASCII);

Read file from classpath with Java 7 NIO

A Path represents a file on the file system. It doesn't help to read a resource from the classpath. What you're looking after is a helper method that reads everything fro a stream (more efficiently than how you're doing) and writes it to a byte array. Apache commons-io or Guava can help you with that. For example with Guava:

byte[] array = 
ByteStreams.toByteArray(this.getClass().getClassLoader().getResourceAsStream(resourceName));

If you don't want to add Guava or commons-io to your dependencies just for that, you can always read their source code and duplicate it to your own helper method.

Java 7: get Path of resource (as object of type Path)

How about

Path path = Paths.get(url.toURI());

It is not proper to create a File from your URL, since it's gotten from the classpath and the file may actually be within a jar.

Cann't get file from classpath (using NIO2)

You should not be trying and accessing resources in your classpath as Paths.

While this will very probably work when your project sits in your IDE setup, it won't as soon as your project is packaged as a jar; it is then impossible to access them using even Path (and even though you can open zip files, therefore jars, as FileSystems).

Use the dedicated methods to do that instead, starting with .getResourceAsStream():

final InputStream in = MyClass.class.getResourceAsStream("/path/to/resource");

Note that you will need to check whether the return code of that method is null (this is what is returned if the resource is not found in the classpath).

How to get the path of src/test/resources directory in JUnit?

Try working with the ClassLoader class:

ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("somefile").getFile());
System.out.println(file.getAbsolutePath());

A ClassLoader is responsible for loading in classes. Every class has a reference to a ClassLoader. This code returns a File from the resource directory. Calling getAbsolutePath() on it returns its absolute Path.

Javadoc for ClassLoader: http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html



Related Topics



Leave a reply



Submit