Try the next:

ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("test.csv");

If the above doesn't work, various projects have been added the following class: ClassLoaderUtil1 (code here).2

Here are some examples of how that class is used:

src\main\java\com\company\test\YourCallingClass.java
src\main\java\com\opensymphony\xwork2\util\ClassLoaderUtil.java
src\main\resources\test.csv
// java.net.URL
URL url = ClassLoaderUtil.getResource("test.csv", YourCallingClass.class);
Path path = Paths.get(url.toURI());
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
// java.io.InputStream
InputStream inputStream = ClassLoaderUtil.getResourceAsStream("test.csv", YourCallingClass.class);
InputStreamReader streamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(streamReader);
for (String line; (line = reader.readLine()) != null;) {
    // Process line
}

Notes

  1. See it in The Wayback Machine.
  2. Also in GitHub.
Answer from Paul Vargas on Stack Overflow
🌐
Mkyong
mkyong.com › home › java › java – read a file from resources folder
Java - Read a file from resources folder - Mkyong.com
September 4, 2020 - In Java, we can use getResourceAsStream or getResource to read a file or multiple files from a resources folder or root of the classpath.
🌐
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 - First, we’ll learn how to load a file from the classpath, a URL, or from a JAR file using standard Java classes. Second, we’ll see how to read the content with BufferedReader, Scanner, StreamTokenizer, DataInputStream, SequenceInputStream, and FileChannel.
🌐
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 - Once we have the InputStream reference, we can use it to read the file content or pass it to any resource handler class. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class ReadFileFromResourcesUsingGetResourceAsStream { public static void main(final String[] args) throws IOException { //Creating instance to avoid static member methods ReadFileFromResourcesUsingGetResourceAsStream instance = new ReadFileFromResourcesUsingGetResourceAsStream(); InputStream is = instance.getFileAsIOStream("demo.txt"); instance.prin
🌐
Davidvlijmincx
davidvlijmincx.com › home › java › read a file from resources directory
Read a file from resources directory
February 14, 2024 - While the path is easier to write the rest of the code is less straightforward. The first example is also much more preferred. The previous example creates an InputStream using the classloader. The file you want to read is specified inside the getResourceAsStream() method as a parameter.
🌐
GeeksforGeeks
geeksforgeeks.org › advance java › read-file-from-resources-folder-in-spring-boot
Read file from Resources Folder in Spring Boot - GeeksforGeeks
November 3, 2025 - ResourceLoader helps load files from the classpath, filesystem, or URL. Always close BufferedReader after reading the file.
🌐
Novixys Software
novixys.com › blog › read-file-resources-folder-java
How to Read a File from Resources Folder in Java | Novixys Software Dev Blog
January 10, 2017 - The following code snippet shows ... not found: " + respath); Using the method Class.getResourceAsStream(String), you can get an InputStream to read the resource....
🌐
GitHub
gist.github.com › Arun205 › 5c3a870ef39f3eea3072d568a5ffc081
Java - Read a file from resources folder · GitHub
Java - Read a file from resources folder. GitHub Gist: instantly share code, notes, and snippets.
Find elsewhere
🌐
amitph
amitph.com › home › java › read a file from resources folder in java
Read a File from Resources Folder in Java - amitph
November 22, 2024 - The following tutorial covers examples of reading files from ‘src/main/resources‘ or ‘src/test/resources‘ directory. Java Class class provides the getResourceAsStream() method that can open an InputStream to a file in the classpath.
🌐
Sde-coursepack
sde-coursepack.github.io › modules › java › File-Resources
SDE | File Resources
I’m going to use my project from the IntelliJ lecture. First, I open up a text editor and write the file contents: A Haiku for my cat: Chloe, calico, Why do you stand on shoulders? I'm trying to work! Now, wherever I saved that file, I’m going to copy and paste (or click and drag) the file into my project’s src/main/resources folder: The following code is a complete program to open and read a resource file: import java.io.*; public class FileResourceExample { private static final String RESOURCE_FILENAME = "chloe.txt"; public static void main(String[] args) { FileResourceExample example
🌐
Oracle
docs.oracle.com › javase › 8 › docs › technotes › guides › lang › resources.html
Location-Independent Access to Resources
March 16, 2026 - Applications use "well known locations" such as System.getProperty("user.home") or System.getProperty("java.home"), then add "/lib/resource", and open that file. Methods in the classes Class and ClassLoader provide a location-independent way to locate resources. For example, they enable locating resources for: An applet loaded from the Internet using multiple HTTP connections.
🌐
Medium
medium.com › @arthurraposodev › test-694cf70bd013
Java Spring Boot: How to Read from a File in Resources | by Arthur Raposo | Medium
November 21, 2023 - ClassPathResource is a part of the Spring framework that provides another way to access files within the classpath, like those in the resources directory. It is particularly useful when dealing with resources in a more direct way. When it’s better: This method is ideal when you need a simple and straightforward approach to access classpath resources without autowiring additional components like ResourceLoader.
🌐
Java2Blog
java2blog.com › home › core java › java file io › read a file from resources folder in java
Read a file from resources folder in java - Java2Blog
January 12, 2021 - In this post, we will see how to read a file from resources folder in java. If you create a maven project(simple java or dynamic web project) , you will see folder src/java/resources.
🌐
Lenar
lenar.io › read-files-from-resource
Read files from resources folder | Lenar.io
The way how we get InputStream allows us to read files even if they are in a jar. This is an “improved” version of the code above. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class ResFile { private final String fileName; public ResFile(String fileName) { this.fileName = fileName; } public List<String> getLines() { List<String> lines = new ArrayList<>(); try (InputStream inputStream = ResFile.class.getResourceAsStream("/"
🌐
JanBask Training
janbasktraining.com › community › devops › reading-a-resource-file-from-within-jar
reading a resource file from within jar | JanBask Training Community
July 5, 2021 - Here's an example of how to read a resource file from within a JAR: import java.io.IOException; import java.io.InputStream; import java.util.Scanner; public class ReadResourceFromJar { public static void main(String[] args) { // Get the InputStream for the resource file InputStream inputStream = ReadResourceFromJar.class.getResourceAsStream("/path/to/resource/file.txt"); if (inputStream != null) { try (Scanner scanner = new Scanner(inputStream)) { // Read the contents of the resource file while (scanner.hasNextLine()) { String line = scanner.nextLine(); System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } else { System.out.println("Resource not found!"); } } }
🌐
CodeSpeedy
codespeedy.com › home › read a file from resource file in java
Read a file from resource file in Java - CodeSpeedy
December 12, 2021 - import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; public class ReadPropertiesFileJavaMain { public static void main(String args[]) throws IOException { ReadPropertiesFileJavaMain rp=new ReadPropertiesFileJavaMain(); System.out.println("Reading a file from theresources folder"); System.out.println("****************************"); rp.readFile("config.txt"); System.out.println("****************************"); } public void readFile(String fileName) throws IOExcepti
🌐
Coderanch
coderanch.com › t › 782645 › java › Unable-read-file-resources-folder
Unable to read file in resources folder from console application, but Eclipse (Java in General forum at Coderanch)
Hi guys, After extensively reading and testing, with my baby demanding attention, I've managed to make it work, finally! It had to do with my environement variable classpath in Window in conjunction with the location the file had. First of all, I set my classpath on my machine to this: classpath=".;C:\Java\eclipse-workspace\GoogleSheets\resources;C:\Java\eclipse-workspace\GoogleSheets\src\main\resources" My file was specified like this in code: Then...
🌐
Java67
java67.com › 2023 › 03 › how-to-read-files-from-resources-folder.html
How to read files from the resources folder in Spring Boot? Example Tutorial | Java67
That's all about 5 ways to read a file from the resource folder in Spring boot application and Java. Thanks for staying with us, with this we reach the end of our article. In this brief blog, we examined several methods for utilizing Spring to access and read a resource via Resource Interface, ClassPathResource, @Value annotation, ResourceLoader, and ResourceUtils.
🌐
HowToDoInJava
howtodoinjava.com › home › spring boot 2 › read a file from resources in spring boot
Read a File from Resources in Spring Boot
August 27, 2023 - We can specify the absolute path to the resource on the file system with prefix 'file:'. ServletContextResource: Loads a resource from the ServletContext. We can specify the path to the resource relative to the ServletContext. It does not use any prefixes. @Autowired ResourceLoader resourceLoader; //Read resource Resource classPathResource = resourceLoader.getResource("classpath:file.txt"); Resource urlResource = resourceLoader.getResource("http://example.com/file.txt"); Resource fileSystemResource = resourceLoader.getResource("file:/path/to/file.txt"); Resource servletContextResource = resourceLoader.getResource("/WEB-INF/file.txt"); //Get file content File file = resource.getFile());
🌐
Delft Stack
delftstack.com › home › howto › java › java read file from resources
How to Read File From Resources in Java | Delft Stack
March 4, 2025 - Learn how to read files from resources in Java with this comprehensive guide. Explore methods like ClassLoader and getResourceAsStream, complete with practical code examples. Enhance your Java skills by mastering file reading techniques for text, JSON, and configuration files.