Nine ways to get files in the resources directory
/** * Read the file content according to the file 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
The main core method is to use the getResource and getPath methods, where getResource("") is an empty string
public void function1(String fileName) throws IOException { String path = this.getClass().getClassLoader().getResource("").getPath();//Note that getResource("") is an empty string System.out.println(path); String filePath = path + fileName; System.out.println(filePath); getFileContent(filePath); }
Method 2
The main core method is to use the getResource and getPath methods to obtain the file path directly through the getResource(fileName) method. Note that if the path contains Chinese, it must be decoded using URLDecoder.decode.
/** * Get the path directly through the file name getPath * * @param fileName * @throws IOException */ 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");//If there is Chinese in the path, it will be URLEncoder, so it needs to be decoded here System.out.println(filePath); getFileContent(filePath); }
way three
Get the file directly by filename+getFile(). If it is a file path, getFile and getPath have the same effect. If it is a URL path, getPath is a path with parameters. As follows:
url.getFile()=/pub/files/foobar.txt?id=123456 url.getPath()=/pub/files/foobar.txt
The code to get the file using getFile() is as follows:
/** * Get it directly through the file name + getFile() * * @param fileName * @throws IOException */ 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");//If there is Chinese in the path, it will be URLEncoder, so it needs to be decoded here System.out.println(filePath); getFileContent(filePath); }
Method 4 (important)
Use the getResourceAsStream method directly to get the stream. The above methods all need to get the file path, but in SpringBoot all files are in the jar package and there is no actual path, so the following methods can be used.
/** * Get the stream directly using the getResourceAsStream method * springboot This method needs to be used in the project, because there is no actual path to store the file in the jar package * * @param fileName * @throws IOException */ public void function4(String fileName) throws IOException { InputStream in = this.getClass().getClassLoader().getResourceAsStream(fileName); getFileContent(in); }
Method 5 (important)
The main method is to use the getResourceAsStream method to obtain the stream. If you do not use getClassLoader, you can use getResourceAsStream("/Configuration Test.txt") to obtain the stream directly from the resources root path. All files in SpringBoot are in the jar package, and there is no actual path, so you can use the following way.
/** * Get the stream directly using the getResourceAsStream method * If you don't use getClassLoader, you can use getResourceAsStream("/config test.txt") to get it directly from the resources root path * * @param fileName * @throws IOException */ public void function5(String fileName) throws IOException { InputStream in = this.getClass().getResourceAsStream("/" + fileName); getFileContent(in); }
Method 6 (important)
The file stream is obtained through the ClassPathResource class. All files in SpringBoot are in the jar package, and there is no actual path, so the following methods can be used.
/** * Obtained through the ClassPathResource class, it is recommended to use it in SpringBoot * springboot This method needs to be used in the project, because there is no actual path to store the file in the jar package * * @param fileName * @throws IOException */ public void function6(String fileName) throws IOException { ClassPathResource classPathResource = new ClassPathResource(fileName); InputStream inputStream = classPathResource.getInputStream(); getFileContent(inputStream); }
way seven
Get the location of the file in the project through the absolute path, which is only a local absolute path and cannot be used for server acquisition.
/** * Get the location of the file in the project by absolute path (cannot be used for server) * @param fileName * @throws IOException */ public void function7(String fileName) throws IOException { String rootPath = System.getProperty("user.dir");//E:\WorkSpace\Git\spring-framework-learning-example String filePath = rootPath + "\\chapter-2-springmvc-quickstart\\src\\main\\resources\\"+fileName; getFileContent(filePath); }
way eight
Obtaining the current absolute path through new File("") is only a local absolute path and cannot be used for server acquisition.
/** * Get the location of the file in the project by absolute path (cannot be used for server) * @param fileName * @throws IOException */ public void function8(String fileName) throws IOException { //parameter is empty File directory = new File(""); //Canonical path: The getCanonicalPath() method returns an absolute path, which parses symbols such as ..\ and .\ String rootCanonicalPath = directory.getCanonicalPath(); //Absolute path: The getAbsolutePath() method returns the absolute path of the file. If it is a full path during construction, it returns the full path directly. If it is a relative path during construction, it returns the path of the current directory + the path when the File object was constructed. String rootAbsolutePath =directory.getAbsolutePath(); System.out.println(rootCanonicalPath); System.out.println(rootAbsolutePath); String filePath = rootCanonicalPath + "\\chapter-2-springmvc-quickstart\\src\\main\\resources\\"+fileName; getFileContent(filePath); }
way nine
Mainly by setting the environment variable and placing the file in the environment variable, the principle is also obtained through the absolute path.
In the example I set an environment variable: TEST_ROOT=E:\WorkSpace\Git\spring-framework-learning-example
System.getenv("TEST_ROOT"); System.getProperty("TEST_ROOT")
By setting environment variables, and then getting files through absolute paths
/** * Get the location of a file in a project by absolute path * * @param fileName * @throws IOException */ public void function9(String fileName) throws IOException { System.setProperty("TEST_ROOT","E:\\WorkSpace\\Git\\spring-framework-learning-example"); //parameter is empty String rootPath = System.getProperty("TEST_ROOT"); System.out.println(rootPath); String filePath = rootPath + "\\chapter-2-springmvc-quickstart\\src\\main\\resources\\"+fileName; getFileContent(filePath); }