Backend Development 9 min read

Nine Ways to Access Files in the resources Directory in Spring Boot

This article explains nine practical methods for reading files placed in the resources folder of a Spring Boot project, covering classloader resource retrieval, URL decoding, getFile(), getResourceAsStream, ClassPathResource, absolute paths, and environment‑variable based approaches, each with code examples.

Top Architect
Top Architect
Top Architect
Nine Ways to Access Files in the resources Directory in Spring Boot

In many Java projects static assets are stored under the resources directory and need to be read at runtime. This guide collects nine ways to obtain files from that directory.

The common utility method used throughout is:

/** * 根据文件路径读取文件内容 * @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 1

Use getResource and getPath with an empty string to obtain the base path, then append the file name.

public void function1(String fileName) throws IOException {
    String path = this.getClass().getClassLoader().getResource("").getPath();
    System.out.println(path);
    String filePath = path + fileName;
    System.out.println(filePath);
    getFileContent(filePath);
}

Method 2

Directly call getResource(fileName) and decode the URL if the path contains Chinese characters.

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");
    System.out.println(filePath);
    getFileContent(filePath);
}

Method 3

Use getFile() (or getPath() ) on the URL returned by getResource . The method works similarly to Method 2.

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 4 (Important)

Use getResourceAsStream to obtain an InputStream . This works in Spring Boot because files are packaged inside the JAR and have no real file system path.

public void function4(String fileName) throws IOException {
    InputStream in = this.getClass().getClassLoader().getResourceAsStream(fileName);
    getFileContent(in);
}

Method 5 (Important)

Also use getResourceAsStream but call it on the class itself, allowing a leading slash to refer to the root of resources .

public void function5(String fileName) throws IOException {
    InputStream in = this.getClass().getResourceAsStream("/" + fileName);
    getFileContent(in);
}

Method 6 (Important)

Leverage Spring's ClassPathResource to obtain an InputStream from the classpath.

public void function6(String fileName) throws IOException {
    ClassPathResource classPathResource = new ClassPathResource(fileName);
    InputStream inputStream = classPathResource.getInputStream();
    getFileContent(inputStream);
}

Method 7

Construct an absolute path using the current working directory ( user.dir ). This works only for local development, not for servers.

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 8

Use new File("") to obtain the current directory, then build the absolute 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 9

Set an environment variable (e.g., TEST_ROOT ) pointing to the project root and build the path from it.

String testRoot = System.getenv("TEST_ROOT"); // or System.getProperty("TEST_ROOT")
String filePath = testRoot + "\\src\\main\\resources\\" + fileName;
getFileContent(filePath);

These nine approaches cover most scenarios for reading resource files in a Spring Boot application, from simple classloader lookups to environment‑variable based absolute paths.

JavaBackend DevelopmentSpring Bootresource loadingFile Access
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.