
1. preface
Java During development, we often need to obtain the path of a file, such as reading configuration files, etc. Today we will briefly discuss the path of files and how to read files.
2. the path to the file
The path to the file usually has relative path with absolute path。
2.1 relative path
Based on the current file, the directory points to the referenced resource file. In Java code, the location of the currently running code is used as the reference location, and the referenced file can be read as long as the location relative to the referenced file remains unchanged. Once the relative position is changed, it cannot be read.
2.2 absolute path
The actual path of a file in the file system refers to the path from the root directory of the hard disk (Windowsis the drive letter), point to files at one level (read and write layer by layer from the root directory). An absolute path, as the name suggests, is an absolute address, just like if you tell someone your house number, he can find your house. Rather than the relative location you told him to be next door to Lao Wang's house.
2.3 path shorthand
We often see that some file directory paths are abbreviated with some symbols, so it is necessary to summarize them here (based on classes UnixSystem as an example):
identifier | description |
---|---|
../ | Represents the directory above the directory where the current file is located |
./ | Represents the directory where the current file is located |
/ | Represents root directory |
~ | Current user directory, mac under /Users/username , and win10underc:\users\username |
Windows Next basic will
/
changed to\
That's enough.
3. Reading files in Java
Let's first declare a test path:
foo
|_src
| |_Test.java
| |_app.yml
wherein Test.java
Used to write reads app.yml
The logic of the document.Java byjava.io.File
to perform file operations. It also provides the following three methods to get the path to the file.
3.1 getPath
This method returns a string form of the abstract path name of the file. This is actually the path name passed to the File constructor.
Therefore, if File
If the object was created using a relative path, the value returned will also be the relative path. If it is an absolute path, return the absolute path.
File file = new File("./ app.yml");
#Output path = ./ app.yml
System.out.println("path = " + file.getPath());
#If it is an absolute path
File file = new File("/Users/dax/IdeaProjects/foo/src/app.yml");
#Enter path = path = /Users/dax/IdeaProjects/foo/src/app.yml
System.out.println("path = " + file.getPath());
3.2 getAbsolutePath
This method returns the absolute path to the file.Please pay attention! There is a big pit here.If your files are in Java Within the project, paths are calculated based on compiled paths.
File file = new File("./ app.yml");
# absolutePath = /Users/dax/IdeaProjects/foo/./ app.yml
System.out.println("absolutePath = " + absolutePath);
At the same time, we found that this method only resolves the relative path of the current directory (the directory where the code above is located), if the path in initialization contains 2.3 chapters of shorthand symbols ,shorthand symbols Will not be parsed.
Because of the existence of shorthand characters, a document in the file system absolute path There can be many.
3.3 getCanonicalPath
shorthand symbols Not being parsed is sometimes painful, and we may need to know the specific path.getCanonicalPath()
Method solves this problem.
File file = new File("./ app.yml");
# canonicalPath = /Users/dax/IdeaProjects/foo/app.yml
System.out.println("canonicalPath = " + file.getCanonicalPath());
due togetCanonicalPath()
You are reading the file system, so performance can be reduced. If we determine that we are not using shorthand characters and the case of the drive letter has been standardized (if usingWindows OS), we should use it firstgetAbsoultePath()
, unless you must use it in your project getCanonicalPath()
。
The canonical path (which does not contain shorthand characters) is unique to a fixed-location document.
read more