Yes, Guava provides this in the Resources class. For example:

CopyURL url = Resources.getResource("foo.txt");
String text = Resources.toString(url, StandardCharsets.UTF_8);
Answer from Jon Skeet on Stack Overflow
🌐
Xenovation
xenovation.com › home › blog › java › java - read file from classpath
Java - Read file from classpath | XENOVATION
April 24, 2019 - Learn how to read a file from the java classpath (resources mostly inside a jar, target or bin folder)
🌐
How to do in Java
howtodoinjava.com › home › i/o › reading a file from classpath in java
Reading a File from Classpath in Java
April 11, 2022 - To read any file from the classpath in a class, we have to get the reference of the system classloader for that class that is trying to read the file. System classloader obviously knows the other paths for the application.
🌐
Baeldung
baeldung.com › home › java › java io › how to read a file in java
How to Read a File in Java | Baeldung
January 8, 2024 - As we can see, the Files API offers another easy way to read the file contents into a String. In the next sections, we’ll look at other less common methods of reading a file that may be appropriate in some situations. Next let’s use a Scanner to read from the File.
🌐
Mkyong
mkyong.com › home › java › java – read a file from resources folder
Java - Read a file from resources folder - Mkyong.com
September 4, 2020 - There is also one that I like the most: File file = ResourceUtils.getFile(“classpath:file/test.txt”) String txt= FileUtils.readFileToString(file); ResourceUtils are in spring and FileUtils are in commons io.
🌐
Simplesolution
simplesolution.dev › java-read-classpath-resource-as-string-using-apache-commons-io
Java Read Classpath Resource as String using Apache Commons IO
For example, we have a file in classpath at src/main/resources/test.txt with the content as below. Simple Solution Java Tutorials Apache Commons IO Tutorials · The Java program below, we use the IOUtils.resourceToByteArray() method to read the ...
Top answer
1 of 16
678

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
2 of 16
137

When using the Spring Framework (either as a collection of utilities or container - you do not need to use the latter functionality) you can easily use the Resource abstraction.

Resource resource = new ClassPathResource("com/example/Foo.class");

Through the Resource interface you can access the resource as InputStream, URL, URI or File. Changing the resource type to e.g. a file system resource is a simple matter of changing the instance.

🌐
Xing's Blog
xinghua24.github.io › Java › Java-Read-Files
Reading a File in Java | Xing's Blog
April 18, 2021 - We can get the classloader of a class and then use getResourceAsStream method to get resource as InputStream. After we get InputStream, we can use InputStream.readAllBytes method to convert InputStream to bytes and create String from the bytes.
🌐
Steranka Blog
steranka.com › 2024 › 10 › 25 › java-read-file-from-classpath.html
Java - Read file from classpath | Steranka Blog
October 25, 2024 - * * @param file The path to the file on the classpath. * @return The content of the file as a String. * @throws IllegalArgumentException If the file is not found on the classpath. */ public static String readResource(String file) { // Try to load the file from the classpath InputStream inputStream ...
Find elsewhere
🌐
W3Docs
w3docs.com › java
How to read text file from classpath in Java?
Here's an example of how you can read a text file from the classpath in Java: InputStream is = getClass().getClassLoader().getResourceAsStream("file.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close();
🌐
Delft Stack
delftstack.com › home › howto › java › java read file from classpath
How to Read File From Classpath in Java | Delft Stack
February 14, 2024 - The file content is read using the Files.readAllLines(filePath) method, which returns a List<String> with each line. To print each line to the console, a forEach loop with System.out::println is employed.
🌐
GeeksforGeeks
geeksforgeeks.org › java › read-a-file-from-the-classpath-in-java
How to Read a File From the Classpath in Java? - GeeksforGeeks
July 23, 2025 - // Java Program to Read a File from the Classpath import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; // Driver Class public class MyClass { // Main Function public static void main(String[] args) { // Filename String filePath = "MyFile.txt"; // Loading the current class's class loader for inputStream InputStream inputStream = MyClass.class.getClassLoader().getResourceAsStream(filePath); // Checking if the class loaded successfully if (inputStream != null) { try (BufferedReader reader = new BufferedReader(new InputStreamReade
🌐
Code Recipes Blog
coderecipes.blog › 2025 › 04 › 27 › reading-classpath-file-contents
Reading classpath file contents – Code Recipes Blog
June 28, 2025 - “IOUtils” class provides a static method named “resourceToString”. This method reads the contents of the file and return a String object. This method takes 3 arguments 1) the file name 2) the ClassLoader instance 3) CharSet · Below ...
🌐
Baeldung
baeldung.com › home › spring › load a resource as a string in spring
Load a Resource as a String in Spring | Baeldung
February 26, 2026 - Here we have called ResourceReader#readFileToString describing the location of the file by using a “classpath:” –prefixed path inside our @Value annotation. To reduce the amount of code in the SpEL, we’ve created a helper method in the class ResourceReader which uses Apache Commons FileUtils to access the file from the path provided: public class ResourceReader { public static String readFileToString(String path) throws IOException { return FileUtils.readFileToString(ResourceUtils.getFile(path), StandardCharsets.UTF_8); } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › loading-resources-from-classpath-in-java-with-example
Loading Resources from Classpath in Java with Example - GeeksforGeeks
July 23, 2025 - String fileName = "GFG_text.txt"; ... given URL URL url = obj.getClass().getClassLoader().getResource(fileName); File file = new File(url.toURI()); // reading the file data // by creating a list of strings // of each line List<String> ...
🌐
Java-success
java-success.com › home › read a text file from java classpath
Read a text file from Java classpath | Big Data & Java Success
October 9, 2015 - Home › Java › Core Java Q&As › Core Java - I/O › Read a text file from Java classpath · File to read: src/main/resources/examples/request.xml · 1. Using Java API without any libraries · You can also use the “ClassLoader” · 2. Using Java 7 NIO ·
🌐
How to do in Java
howtodoinjava.com › home › i/o › read a file from resources directory
Read a File from Resources Directory - HowToDoInJava
December 8, 2022 - mvn clean package java -classpath "classes/." com.howtodoinjava.io.ReadFileFromResourcesUsingGetResource Content from demo.txt Content from data/demo.txt · If the application is a Spring WebMVC or Spring Boot application then we may take advantage of org.springframework.util.ResourceUtils class. File file = ResourceUtils.getFile("classpath:demo.txt") //File is found System.out.println("File Found : " + file.exists()); //Read File Content String content = new String(Files.readAllBytes(file.toPath())); System.out.println(content); Happy Learning !!