Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read the file path under the resources directory by Java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to read the file path under the resources directory by Java". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how to read the file path under the resources directory by Java"!

The way to print a file is as follows:

/ * * read file contents 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 ();} Mode one

The main core method is to use the getResource and getPath methods, where the getResource ("") contains empty strings.

Public void function1 (String fileName) throws IOException {String path = this.getClass () .getClassLoader () .getResource (") .getPath (); / / notice that getResource (") contains the empty string System.out.println (path); String filePath = path + fileName; System.out.println (filePath); getFileContent (filePath);} mode two

The main core method is to use the getResource and getPath methods to obtain the file path directly through the getResource (fileName) method. Be sure to use URLDecoder.decode decoding if the path contains Chinese.

/ * obtain the path * * @ param fileName * @ throws IOException * / public void function2 (String fileName) throws IOException {String path = this.getClass () .getClassLoader () .getResource (fileName) .getPath () directly through the file name getPath; / / notice that getResource (") contains the empty string System.out.println (path); String filePath = URLDecoder.decode (path," UTF-8 ") / / if there is Chinese in the path, it will be URLEncoder, so you need to decode System.out.println (filePath); getFileContent (filePath);} mode 3

Get the file directly through the file name + getFile (). If it is a file path, then getFile and getPath effect is the same, if it is a URL path, then getPath is a path with parameters. As follows:

Url.getFile () = / pub/files/foobar.txt?id=123456url.getPath () = / pub/files/foobar.txt

The code to get the file using the getFile () method is as follows:

/ * get * * @ param fileName * @ throws IOException * / public void function3 (String fileName) throws IOException {String path = this.getClass () .getClassLoader () .getResource (fileName) .getFile () directly through the file name + getFile (); / / notice that getResource (") contains the empty string System.out.println (path); String filePath = URLDecoder.decode (path," UTF-8 ") / / if there is Chinese in the path, it will be URLEncoder, so you need to decode System.out.println (filePath); getFileContent (filePath);} mode 4 (important)

Directly use the getResourceAsStream method to get the stream, all of the above methods need to get the file path, but in SpringBoot all the files are in the jar package, there is no actual path, so you can use the following methods.

/ * * directly use the getResourceAsStream method to get streams * this method is required in springboot projects because there is not an actual path in the jar package to store the file * * @ param fileName * @ throws IOException * / public void function4 (String fileName) throws IOException {InputStream in = this.getClass (). GetResourceAsStream (fileName); getFileContent (in);} Mode five (important)

The main way to get the stream is to use the getResourceAsStream method. Without using getClassLoader, you can use getResourceAsStream ("/ configuration test .txt") to get it directly under the resources root path. All the files in SpringBoot are in the jar package, so you can use the following methods.

/ * * directly use the getResourceAsStream method to obtain streams * if you do not use getClassLoader, you can use getResourceAsStream ("/ configuration test .txt") to obtain * * @ param fileName * @ throws IOException * / public void function5 (String fileName) throws IOException {InputStream in = this.getClass (). GetResourceAsStream ("/" + fileName); getFileContent (in);} mode six (important)

Get the file stream through the ClassPathResource class, and all the files in SpringBoot are in the jar package without an actual path, so you can use the following ways.

/ * is obtained through the ClassPathResource class. It is recommended to use this method in * springboot projects in SpringBoot, because there is not an actual path in the jar package to store files * * @ param fileName * @ throws IOException * / public void function6 (String fileName) throws IOException {ClassPathResource classPathResource = new ClassPathResource (fileName); InputStream inputStream = classPathResource.getInputStream (); getFileContent (inputStream);} method 7

Get the location of the files in the project through the absolute path, which is only the local absolute path and cannot be used for server acquisition.

/ * obtain the location of the files in the project through the absolute path (cannot be used on the 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);} mode eight

Get the current absolute path through new File ("), which is only a local absolute path and cannot be used for server acquisition.

/ * get the location of the files in the project through the absolute path (cannot be used on the server) * @ param fileName * @ throws IOException * / public void function8 (String fileName) throws IOException {/ / the parameter is empty File directory = new File ("") / / canonical path: the getCanonicalPath () method returns an absolute path and parses symbols such as..\,.\ String rootCanonicalPath = directory.getCanonicalPath () / / absolute path: the getAbsolutePath () method returns the absolute path of the file. If it is constructed, it directly returns the full path. If it is a relative path, it returns the path of the current directory + the path of the File object 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);} method 9

Mainly by setting the environment variable, the file is placed in the environment variable, and 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 the environment variable, and then getting the file through the absolute path

/ * obtain the location of the files in the project through the absolute path * * @ param fileName * @ throws IOException * / public void function9 (String fileName) throws IOException {System.setProperty ("TEST_ROOT", "E:\ WorkSpace\\ Git\\ spring-framework-learning-example"); / / the 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); at this point, I believe you have a better understanding of "how to read the file path under the resources directory by Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report