In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "java several common methods to obtain file paths", the explanation content in the article is simple and clear, easy to learn and understand, please follow the small series of ideas slowly in-depth, together to study and learn "java several common methods to obtain file paths" bar!
There are several common ways to get file paths in Java. Here are some examples of how to use them and how they work.
The directory structure of test items is shown in the right figure:
1. Use the File class
File file = new File("");// Java projects-->D:\WorkSoftware\Workspace\Eclipse Projects\MyWorkspace\test// Web projects-->D:\WorkSoftware\Server\apache-tomcat-7.0.96\binSystem.out.println(file.getAbsolutePath());File file = new File("/");/ Java projects--> D: \/ Web projects-->D:\System.out.println(file.getAbsolutePath());
2. Use of environmental variables
String path = System.getProperty("user.dir");// Java projects-->D:\WorkSoftware\Workspace\Eclipse Projects\MyWorkspace\test/Web projects-->D:\WorkSoftware\Server\apache-tomcat-7.0.96\binSystem.out.println(path);
3. Use the class loader
String path = MyTest.class.getResource("").getPath();// Java project-->D:/WorkSoftware/Workspace/Eclipse Projects/MyWorkspace/test/target/classes/com/luther/test//// Web project-->D: The requested URL/webapps/test/WEB-INF/classes/com/luther/test/System.out.println(path) was not found on this server. String path = MyTest.class.getResource("/").getPath();// Java project-->D:/WorkSoftware/Workspace/Eclipse Projects/MyWorkspace/test/target/classes//// Web project-->D: The requested URL/webapps/test/WEB-INF/classes/System.out.println(path) was not found on this server. String path = MyTest.class.getClassLoader().getResource("").getPath();// Java project-->D:/WorkSoftware/Workspace/Eclipse Projects/MyWorkspace/test/target/classes//// Web project-->D:/WorkSoftware/Server/apache-tomcat-7.0.96/webapps/test/WEB-INF/classes/System.out.println(path);
summary
In Java and Web environments, it is best to use class loading to find files, which is better.
Thank you for reading, the above is "java several common methods to obtain file path" content, after the study of this article, I believe we have a deeper understanding of java several common methods to obtain file path this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.