1. Introduction
In Java development, we often need to get the path of a file, such as reading configuration files and so on. Today we’ll talk briefly about the path to a file and how to read it.
2. File path
File paths usually have relative paths and absolute paths.
2.1 Relative Path
Level by level to the referenced resource file based on the current file. In Java code, the referenced file can be read as long as the position of the referenced file is unchanged relative to the current running code. Once the relative position is changed, it cannot be read.
2.2 Absolute Path
The actual path of a file in a file system refers to the path from the root directory (Windows drive letter) of the hard disk to the file (read and write from the root directory). An absolute path, as the name implies, is an absolute address, just as you can tell someone your house number and they can find your house. Not relative position you told him next door to Lao Wang’s.
2.3 Path shorthand
We often see file directory paths abbreviated with symbols, so it’s worth summarizing (using Unix-like systems as an example) :
identifier | instructions |
---|---|
../ | Represents the upper level of the directory where the current file resides |
. / | Indicates the directory where the current file resides |
/ | Represents the root directory |
~ | Current user directory,macfor/Users/username And thewin10forc:\users\username |
On Windows, change/to \.
3. Read files from Java
Let’s start by declaring a test path:
foo
|_src
| |_Test.java
| |_app.yml
Copy the code
Test.java is used to write the logic to read the app.yml file. In Java, File operations are performed through java.io.File. The following three methods are provided to obtain the file path.
3.1 getPath
This method returns a string representation of the file’s abstract pathname. It is actually the pathname passed to the File constructor.
Therefore, if the File object is created using a relative path, the value returned will also be a relative path. Return the absolute path if it is an absolute path.
File file = new File("./app.yml"); /app.yml system.out. println("path = "+ file.getPath()); # if absolute path to the File to the File = new File ("/Users/dax IdeaProjects/foo/SRC/app. Yml "); Output path = # path = / Users/dax/IdeaProjects/foo/SRC/app. Yml System. Out. The println (" path = "+ file. GetPath ());Copy the code
3.2 getAbsolutePath
This method returns the absolute path to the file. ** Attention please! There are big holes here. ** If your file is in a Java project, the path is calculated as compiled.
File file = new File("./app.yml");
# absolutePath = /Users/dax/IdeaProjects/foo/./app.yml
System.out.println("absolutePath = " + absolutePath);
Copy the code
We also found that this method only resolves relative paths to the current directory (the directory where the code above resides). If the initialized path contains the shorthand characters in Section 2.3, the shorthand characters will not be resolved.
Because of shorthand, there can be many absolute paths to a file in the file system.
3.3 getCanonicalPath
Not being able to parse shorthand is sometimes painful, and we may need to know the exact path. The getCanonicalPath() method solves this problem.
File file = new File("./app.yml");
# canonicalPath = /Users/dax/IdeaProjects/foo/app.yml
System.out.println("canonicalPath = " + file.getCanonicalPath());
Copy the code
Since getCanonicalPath() reads from the file system, it degrades performance. If we are sure that we are not using shorthand and that drive letter case has been standardized (if using Windows OS), we should prefer getAbsoultePath(), unless you must use getCanonicalPath() in your project.
The canonical path (without shorthand) is unique to a fixed-location file.
Follow our public id: Felordcn for more information
Personal blog: https://felord.cn