Get the App
SLTechnology News&Howtos  ›  Development  › 

How Spring boot manipulates files

Shulou Source: shulou.com Published: 2022-06-02 19:41:03 09月18日 Update

Editor to share with you how to operate the Spring boot file, I hope you will learn something after reading this article, let's discuss it together!

First, get the file path

Get file path

1. Class.getResource (path)

The parameter path has two forms, one begins with "/" and the other does not start with "/"

"representation starting with'/'": get the file from the root path of the project, that is, under the classPath directory.

Do not start with "/": search with the location of this type of object * * as the root path.

/ / 1. Get the path System.out.println (this.getClass (). GetResource ("). GetPath ()) of the current file; / / 2. Get the classpath path System.out.println (this.getClass () .getResource ("/") .getPath ()) under target

The use of the class.getResource () and class.getResourceAsStream () methods is consistent in the path.

2. ClassLoader.getResource (path) / 3. It is also the absolute path to get classpath System.out.println (Thread.currentThread (). GetContextClassLoader (). GetResource ("). GetPath ()); / / 4. It is also the absolute path to get classpath System.out.println (this.getClass (). GetClassLoader (). GetResource ("). GetPath ()); / / 5. It is also the absolute path to get classpath System.out.println (ClassLoader.getSystemResource ("). GetPath ())

3. Project path / / 6. Gets the current project path (this method has the same effect as 7, but can change the path to standard form and will handle. " And "..") System.out.println (new File ("). GetCanonicalPath ()); / / 7. Get the absolute path of the project (will not be processed. " And "..") System.out.println (new File (") .getAbsolutePath ()); / / 8.user.dirSystem.out.println (System.getProperty (" user.dir "))

2. Three ways of operating files 1. ClassPath

Read the configuration file under resources [files can only be Properties, xml, JSON]

/ / read file information under ClassPath / / 1, classloader InputStream resourceAsStream = DaoFactory.class.getClassLoader () .getResourceAsStream ("data.properties"); / / 2, current thread loader lassLoader classLoader = Thread.currentThread () .getContextClassLoader (); InputStream is = classLoader.getResourceAsStream (path); 2. FileSystem

Read the file information by specifying the file path, and read the files in the static folder under resources

String path = Thread.currentThread (). GetContextClassLoader (). GetResource ("static/internal.csv"). GetPath (); File file = new File (path); 3. UrlResource

Read the cloud service files through HTTP, and we can also put the configuration files on GitHub or Gitee.

URLConnection con = this.url.openConnection (); InputStream inputStream = con.getInputStream (); String content = IoUtil.readUtf8 (inputStream); System.out.println (content); after reading this article, I believe you have a certain understanding of "how to operate Spring boot document". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!

Tags: File path beginning method project information form location article processing configuration same consistent location parameter done object effect folder more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB NVidia Huawei Linux vpn