The source code is for testing only and should not be used commercially. The source code comes from the Internet. If there is any infringement, please contact me to remove it.
java获取文件路径

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 symbolsshorthand 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
Resource download
PriceFree
The use is limited to testing, experiments, and research purposes. It is prohibited for all commercial operations. This team is not responsible for any illegal behavior of users during use. Please self-test all source codes! There is no guarantee of the integrity and validity of your source code. All source code is collected from the entire network
Original link:https://www.bcbccb.cn/en/4459.html, please indicate the source for reprinting. Disclaimer: This resource has not been authorized by the original rights holder and is not commercially available. It can only be used to learn and analyze the underlying code, CSS, etc., and is prohibited for commercial purposes. Any relevant disputes and legal liabilities arising from unauthorized commercial use shall be fully borne by the user. Everyone is responsible to support genuine copies. Please delete them within 24 hours after downloading. Thank you for your support!
1
美团代付 支持多模板全开源 多种支付通道 多模版三合一源码 附教程
Meituan payment supports multiple templates, full open source, multiple payment channels, multiple templates, three-in-one source code with tutorials
Someone bought it 10 minutes ago Go and have a look

Site Announcements

The source code (theme/plug-in/application source code) and other resources provided by this site are only for learning and exchange

Commercial use is prohibited, otherwise all consequences will be borne by the downloading user!

Some resources are collected or copied online. If they infringe on your legitimate rights and interests, please write to us.

Currently, members have a big reward, and the current price for a lifetime member is 299 gold coins.Recent price adjustments

Join quickly, opportunities wait for no one! immediately participated in

Captcha

Fast login to social accounts

en_USEnglish