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".
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".
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(); }
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.
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(); }
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"); }
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.
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.
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!