Java Project get file path summary

Source: Internet
Author: User
Tags log4j

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Directory (?) [+]

java获取文件路径的方式比较多,总结可能有疏漏。
1, Java.lang.System.getProperty (String key)
System.getProperty("user.dir")这个方法的作用可以获取当前工程的根目录。![我的一个项目](http://img.blog.csdn.net/20160322141415562)

For example, get the root directory of the project: C:\Users\Administrator\Desktop\Dictionary; Use this method, regardless of where your project is placed, has been obtained to the current root directory.
If you need to get other properties of the current project, such as the JDK version, run the system version, and so on, use System.out.println (System.getproperties ()) to output all the properties and see what you need.
So, if we are going to get the files in the images file, we can get the file path via System.getproperty ("User.dir") + "\\images\\user.jpg" (double slash because "\" in Java in the literal character, so "\" only means "\"), the output will be:
C:\Users\Administrator\Desktop\Dictionary\images\user.jpg.

2, Java.io.File.File (String pathname)
1. The first file directory = new file ("");//set to the current folder System.out.println (Directory.getcanonicalfile ());//return type is File System.out.println (Directory.getcanonicalpath ());//Gets the standard path, the return type is String System.out.println (directory.getab Solutefile ());//return type is File System.out.println (Directory.getabsolutepath ());//Get absolute path, return type is string output: C:\users\administrator\desktop\dictionary C:\users\administrator\desktop\dictionary C:\users\administrator\desktop\dictionary C:\users\administrator\desktop\dictionary2, the second file directory = new file ("."); /set to current folder System.out.println (Directory.getcanonicalfile ());//return type is File System.out.println ( Directory.getcanonicalpath ());//Gets the standard path, the return type is String System.out.println (Directory.getabsolutefile ());//return type is file System.out.println (Directory.getabsolutepath ());//Get absolute path, return type is string output: C:\users\administrator\desktop\dictionary C:\users\administrator\desktop\dictionary C:\users\administrator\desktop\dictionary\. C:\users\administrator\desktop\dictionary\.3, third file directory = new file (".."); /set to current folder System.out.println (Directory.getcanonicalfile ());//return type is File System.out.println ( Directory.getcanonicalpath ());//Gets the standard path, the return type is String System.out.println (Directory.getabsolutefile ());//return type is file System.out.println (Directory.getabsolutepath ());//Get absolute path, return type is string output: C:\users\administrator\Desktop C : \users\administrator\Desktop C:\users\administrator\ Desktop\dictionary\. C:\users\administrator\ Desktop\dictionary\:       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

For the Getcanonicalpath () function, the "." Represents the current folder, while the ".." Represents the top-level folder in the current folder
For the GetAbsolutePath () function, regardless of ".", "..", returns the current path plus the path you set at New File ()

For string Java.io.File.getPath () This method:

        File directory =Newfile (out.println (Directory.getpath ());//Output the given path string File directory = new file ( "."); System. out.println (Directory.getpath ());//Output the given path string File directory = new file ( ".."); System. out.println (Directory.getpath ());//Output the given path string File directory = new file ( "images\ User.jpg "); System. out.println (Directory.getpath ());//output of the given path string output: (empty) ... images\user.jpg   
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

Explain to what, File.getpath () output what.

3 There are three ways to get resources based on the classpath:
URL url = This. GetClass (). GetResource ("Resource_name"); URL url = This. GetClass (). getClassLoader (). GetResource ("Resource_name"); URL url = Thread. CurrentThread ().getcontextclassloader () .getResource ( "resource_name") .class.getclassloader () .getclass () .getresource (  "/images/user.jpg") .class.getclassloader () .getclass () .getclassloader () .getresource ( "/images/user.jpg") in a static context".  
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

The first is through the GetResource method of the Class class instance, and the next two kinds are the getresource methods of the ClassLoader class instance. Class.getResource () is also the GetResource method of delegating ClassLoader. So, first say ClassLoader GetResource method:

(1) The GetResource method parameter of ClassLoader cannot begin with "/" and must be searched from the root directory.
The root directory here is the directory in Classpath and the jar that contains the reference. For example, the default for Eclipse is to set the classpath of the Java class runtime in each project to the project root directory/bin directory and all jar packages referenced in the project. At compile time, Copy the SRC directory structure to the bin directory, compile the Java class into a class file, and copy the original directory structure in SRC to the bin directory, along with other files.
Suppose a factory has the following classpath (two):
/bin
Log4j-1.2.16.jar
Where Log4j-1.2.16.jar has the directory structure org\apache\log4j\ (with the package org.apach.log4j), then Find the Test.txt file under the Bin directory using the following method Classloader.getresource ("Test.txt"); note that the Classloader.getresource method must be entered from the root directory, The root directory here is the/bin in Classpath. Find bin/level1/level2/ll.txt files must use Classloader.getresource ("Level1/level2/ll.txt");//Note that the lookup must be based on the root directory (/bin), and that the directory structure is also written to, Cannot use/start.

(2) Class.getResource () slightly different:

(a) The relative path can be found, relative to the current instance of the class file is located in the package, (b) and Classloader.getresource () can also start from the root directory (CLASSPATH) to find,
但是此时传递给Class.getResource()的参数必须要用 "/" 开头,否则就是相对查找了((a)中的情况)其实,这种代码就是将/去掉,然后调用ClassLoader.getResource()

Java Project get file path summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.