9 Ways to Read Resource Files in Spring Boot Applications
This article presents nine practical methods for loading files from the resources directory in a Spring Boot project, covering ClassLoader, Class, Spring ResourceLoader, ResourceUtils, ApplicationContext, ServletContext, Java IO, NIO, and ClassPathResource approaches.
1. Use ClassLoader.getResourceAsStream()
Use the class loader to obtain an InputStream for a resource file. The method takes a resource path and returns an InputStream object.
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("file.txt");Note that the path is relative to the class loader root, so files under the resources directory need the prefix classpath:, e.g., classpath:file.txt.
2. Use Class.getResourceAsStream()
Use getResourceAsStream() of the Class object to read a resource file. It also returns an InputStream.
InputStream inputStream = getClass().getResourceAsStream("/file.txt");The path is relative to the current class, so a leading “/” is required for resources in the root of the classpath.
3. Use Spring's ResourceLoader
Inject a ResourceLoader and call its getResource() method, which returns a Resource that can provide an InputStream.
Resource resource = resourceLoader.getResource("classpath:file.txt");
InputStream inputStream = resource.getInputStream();Remember to autowire the ResourceLoader in the class.
4. Use ResourceUtils
ResourceUtils.getFile()can obtain a File object for a classpath resource.
File file = ResourceUtils.getFile("classpath:file.txt");This works for files on the local file system or inside JARs, but not for WAR files.
5. Use ApplicationContext
Inject ApplicationContext and call getResource() to obtain a Resource and then an InputStream.
Resource resource = applicationContext.getResource("classpath:file.txt");
InputStream inputStream = resource.getInputStream();6. Use ServletContext
Inject ServletContext and call getResourceAsStream() with the path inside /WEB-INF/classes/.
InputStream inputStream = servletContext.getResourceAsStream("/WEB-INF/classes/file.txt");7. Use the File API
Read the file directly with File and FileInputStream by providing the absolute path.
File file = new File("src/main/resources/file.txt");
InputStream inputStream = new FileInputStream(file);8. Use Java NIO Paths and Files
Read the resource with Paths.get() and Files.newInputStream(), requiring the full file path.
Path path = Paths.get("src/main/resources/file.txt");
InputStream inputStream = Files.newInputStream(path);9. Use ClassPathResource
Spring's ClassPathResource loads a resource from the classpath without needing a full path.
ClassPathResource resource = new ClassPathResource("file.txt");
InputStream inputStream = resource.getInputStream();All nine methods can read files located in the resources directory of a Spring Boot project; choose the one that best fits your scenario.
Recommended approaches
Use ClassLoader.getResourceAsStream() for a general solution.
Use ResourceLoader when working within Spring.
Use ClassPathResource for simple classpath file access.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
