9 Proven Ways to Read Files from the Resources Folder in Spring Boot
Learn nine practical methods to access files in a Java project's resources directory—including getResource, getPath, getFile, getResourceAsStream, ClassPathResource, and absolute path techniques—complete with code examples and tips for handling Chinese characters and Spring Boot jar packaging.
This article provides nine approaches to read files from the resources directory in a Java/Spring Boot project, plus a helper method to output file content.
/**
* Read file content by path
*
* @param fileInPath
* @throws IOException
*/
public static void getFileContent(Object fileInPath) throws IOException {
BufferedReader br = null;
if (fileInPath == null) {
return;
}
if (fileInPath instanceof String) {
br = new BufferedReader(new FileReader(new File((String) fileInPath)));
} else if (fileInPath instanceof InputStream) {
br = new BufferedReader(new InputStreamReader((InputStream) fileInPath));
}
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}Method One
Uses getClass().getClassLoader().getResource("") to obtain the base path, then appends the file name.
public void function1(String fileName) throws IOException {
String path = this.getClass().getClassLoader().getResource("").getPath(); // empty string
System.out.println(path);
String filePath = path + fileName;
System.out.println(filePath);
getFileContent(filePath);
}Method Two
Calls getResource(fileName) directly; if the path contains Chinese characters, decode it with URLDecoder.
public void function2(String fileName) throws IOException {
String path = this.getClass().getClassLoader().getResource(fileName).getPath();
System.out.println(path);
String filePath = URLDecoder.decode(path, "UTF-8"); // decode Chinese characters
System.out.println(filePath);
getFileContent(filePath);
}Method Three
Retrieves the file via getFile(); for URL paths, getPath() returns the path without query parameters.
public void function3(String fileName) throws IOException {
String path = this.getClass().getClassLoader().getResource(fileName).getFile();
System.out.println(path);
String filePath = URLDecoder.decode(path, "UTF-8");
System.out.println(filePath);
getFileContent(filePath);
}Method Four (Important)
Uses getResourceAsStream to obtain an InputStream, which works when files are packaged inside a JAR.
public void function4(String fileName) throws IOException {
InputStream in = this.getClass().getClassLoader().getResourceAsStream(fileName);
getFileContent(in);
}Method Five (Important)
Calls getResourceAsStream on the class itself, allowing a leading slash to reference the root of resources.
public void function5(String fileName) throws IOException {
InputStream in = this.getClass().getResourceAsStream("/" + fileName);
getFileContent(in);
}Method Six (Important)
Leverages Spring's ClassPathResource to obtain an input stream, suitable for Spring Boot applications.
public void function6(String fileName) throws IOException {
ClassPathResource classPathResource = new ClassPathResource(fileName);
InputStream inputStream = classPathResource.getInputStream();
getFileContent(inputStream);
}Method Seven
Builds an absolute path using the project's root directory; this works only locally and not on a server.
public void function7(String fileName) throws IOException {
String rootPath = System.getProperty("user.dir");
String filePath = rootPath + "\\chapter-2-springmvc-quickstart\\src\\main\\resources\\" + fileName;
getFileContent(filePath);
}Method Eight
Uses new File("") to get the current directory's canonical path, then constructs the absolute file path.
public void function8(String fileName) throws IOException {
File directory = new File("");
String rootCanonicalPath = directory.getCanonicalPath();
String filePath = rootCanonicalPath + "\\chapter-2-springmvc-quickstart\\src\\main\\resources\\" + fileName;
getFileContent(filePath);
}Method Nine
Sets an environment variable (e.g., TEST_ROOT) and builds the file path from it.
public void function9(String fileName) throws IOException {
System.setProperty("TEST_ROOT", "E:\\WorkSpace\\Git\\spring-framework-learning-example");
String rootPath = System.getProperty("TEST_ROOT");
System.out.println(rootPath);
String filePath = rootPath + "\\chapter-2-springmvc-quickstart\\src\\main\\resources\\" + fileName;
getFileContent(filePath);
}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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
