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
- See it in The Wayback Machine.
- Also in GitHub.
Top answer 1 of 16
374
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
- See it in The Wayback Machine.
- Also in GitHub.
2 of 16
126
Try:
InputStream is = MyTest.class.getResourceAsStream("/test.csv");
IIRC getResourceAsStream() by default is relative to the class's package.
As @Terran noted, don't forget to add the / at the starting of the filename
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.
Videos
04:34
How to Read Files from Resources Folder in Spring Boot: A ...
java read text file from resources
03:21
Java :How do I load a file from resource folder?(5solution) - YouTube
06:54
How to READ FILES with Java in 8 minutes! 📖 - YouTube
28:47
Java I/O File Handling - Locating the Resource: File (Java IO) ...
04:02
How to read a properties xml file from resources in java - YouTube
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.
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.
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());