Home >Java >javaTutorial >How does the springboot project read files in the resources directory?

How does the springboot project read files in the resources directory?

WBOY
WBOYforward
2023-05-19 08:29:201317browse

1: Use the ClassLoader.getResourceAsStream() method

You can use the class loader to obtain the input stream of the resource file. This method requires passing in a resource file path as a parameter, and then returns an InputStream object.

InputStream inputStream = getClass().getClassLoader().getResourceAsStream("file.txt");

Note that the resource file path returned by this method is relative to the root path of the class loader. Therefore, for files in the resources directory, you need to prefix the file name with "classpath:". For example: "classpath:file.txt".

2: Use the Class.getResourceAsStream() method

To read resource files, you can use the getResourceAsStream() method of the Class class. This method requires inputting the path to a resource file and returns an InputStream object.

InputStream inputStream = getClass().getResourceAsStream("/file.txt");

The resource file path returned by this method is relative to the path of the current class. Therefore, for files in the resources directory, you need to add "/" prefix before the file name. For example: "/file.txt".

3: Use ResourceLoader to load files

Use Spring's ResourceLoader interface to load resource files. The ResourceLoader interface has a getResource() method, which accepts a resource file path parameter and returns a Resource object.

Resource resource = resourceLoader.getResource("classpath:file.txt");
InputStream inputStream = resource.getInputStream();

It should be noted that the ResourceLoader object needs to be injected into the class and used in the method. For example:

@Autowired
private ResourceLoader resourceLoader;

public void readResourceFile() throws IOException {
    Resource resource = resourceLoader.getResource("classpath:file.txt");
    InputStream inputStream = resource.getInputStream();
}

4: Use ResourceUtils to load files

Spring provides the ResourceUtils tool class, which can be used to load resource files. To obtain a file object, use the ResourceUtils.getFile() method.

File file = ResourceUtils.getFile("classpath:file.txt");

It should be noted that this method only applies to local file systems and JAR files. This method may not work when working with WAR files or other types of files.

5: Load files using ApplicationContext

To load resource files, you can use the getResource() method in ApplicationContext. A method that accepts a resource file path as a parameter and returns a Resource object.

Resource resource = applicationContext.getResource("classpath:file.txt");
InputStream inputStream = resource.getInputStream();

It should be noted that the ApplicationContext object needs to be injected into the class and used in the method. For example:

@Autowired
private ApplicationContext applicationContext;

public void readResourceFile() throws IOException {
    Resource resource = applicationContext.getResource("classpath:file.txt");
    InputStream inputStream = resource.getInputStream();
}

6: Use ServletContext to load files

You can use the getResourceAsStream() method of ServletContext to read resource files. The parameter of this function is the resource file path and returns an InputStream object.

InputStream inputStream = servletContext.getResourceAsStream("/WEB-INF/classes/file.txt");

It should be noted that the ServletContext object needs to be injected into the class and used in the method. For example:

@Autowired
private ServletContext servletContext;

public void readResourceFile() throws IOException {
    InputStream inputStream = servletContext.getResourceAsStream("/WEB-INF/classes/file.txt");
}

7: Use File System to load files

You can use the File class to read resource files. The full file path is required.

File file = new File("src/main/resources/file.txt");
InputStream inputStream = new FileInputStream(file);

It should be noted that using this method requires providing the complete file path, so you need to know the absolute path where the file is located.

8: Use Paths and Files to load files

In Java NIO, you can read resource files with the help of the Paths and Files classes. This method requires the full file path.

Path path = Paths.get("src/main/resources/file.txt");
InputStream inputStream = Files.newInputStream(path);

It should be noted that using this method requires providing the complete file path, so you need to know the absolute path where the file is located.

9: Use ClassPathResource to load files

Use the ClassPathResource class provided by Spring to read resource files. This method requires the relative path of the resource file.

ClassPathResource resource = new ClassPathResource("file.txt");
InputStream inputStream = resource.getInputStream();

It should be noted that ClassPathResource will search for resource files on the class path, so there is no need to provide the complete file path.

The above is the detailed content of How does the springboot project read files in the resources directory?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete