In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.