Written in the previous chapter: Therefore, without accumulating small steps, you cannot reach a thousand miles; without accumulating small streams, you cannot form a river and sea.
Personal insights:
Recently, there are many demands for digital warehouses, and there are more large-structured jsons or strings. In order to save time, in the past, the local mock data was directly read and debugged from the database or the third-party SDK. It's really efficient, it can be self-tested by starting the project, unit tests or main functions. Record this experience and share it with everyone, and leave it for yourself to spare
Read resource resource file MOCK data in SpringBoot
Debugging interface
In Spring Boot projects, static resources or configuration files are usually placed insrc/main/resources
In the directory. Here is how to read and store it inresources
Steps and code examples for text files in a directory:
File path
Put the file insrc/main/resources
In the directory, for example, the file name ismock_data.txt
。
Code Example
Can be used via SpringClassPathResource
Or standardClassLoader
Come to readresources
contents of the file.
Method 1: UseClassPathResource
import ; import ; import ; import ; public class FileUtil { public static String readFileFromResources(String fileName) { StringBuilder content = new StringBuilder(); try { // Get file resources ClassPathResource resource = new ClassPathResource(fileName); try (BufferedReader reader = new BufferedReader( new InputStreamReader((), StandardCharsets.UTF_8))) { String line; while ((line = ()) != null) { (line).append("\n"); } } } catch (Exception e) { (); } return (); } }
Method 2: UseClassLoader
import ; import ; import ; public class FileUtil { public static String readFileFromResources(String fileName) { StringBuilder content = new StringBuilder(); try { // Get the class loader of the current thread ClassLoader classLoader = ().getContextClassLoader(); try (BufferedReader reader = new BufferedReader( new InputStreamReader((fileName), StandardCharsets.UTF_8))) { String line; while ((line = ()) != null) { (line).append("\n"); } } } catch (Exception e) { (); } return (); } }
Example usage
public class Main { public static void main(String[] args) { String content = ("mock_data.txt"); (content); } }
Things to note
File path:
- If the file is located
resources
In the subdirectory ofdata/mock_data.txt
,SofileName
Should be set to"data/mock_data.txt"
。
File existence:
- make sure
mock_data.txt
Has been correctly placed insrc/main/resources
or its subdirectory. - If the file does not exist or the path is wrong, it will be thrown
FileNotFoundException
。
Coding issues:
- The character set of the file should be specified when reading (such as
UTF-8
) to avoid garbled code problems.
With these methods, you can easily read and reference Spring Boot projectsresources
File contents in the directory.
This is the end of this article about reading mock data (efficient debugging interface) in SpringBoot. For more related SpringBoot, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!