黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

專注Java教育14年 全國(guó)咨詢/投訴熱線:400-8080-105
動(dòng)力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學(xué)習(xí)攻略 Java學(xué)習(xí) Java獲取文件路徑的方法

Java獲取文件路徑的方法

更新時(shí)間:2022-06-10 11:19:34 來源:動(dòng)力節(jié)點(diǎn) 瀏覽2847次

在 Java 中,對(duì)于 NIO Path,我們可以使用path.toAbsolutePath()來獲取文件路徑;對(duì)于 legacy IO File,我們可以file.getAbsolutePath()用來獲取文件路徑。

對(duì)于符號(hào)鏈接或文件路徑包含.or ..,我們可以使用path.toRealPath()orfile.getCanonicalPath()來獲取真正的文件 pah。

下面是File映射Path。

file.getAbsolutePath()<=>path.toAbsolutePath()

file.getCanonicalPath()<=>path.toRealPath()

file.getParent()<=>path.getParent()

1.獲取文件的文件路徑(NIO Path)。

對(duì)于java.nio.file.Path,我們可以使用以下 API 來獲取文件的文件路徑。

path.toAbsolutePath()– 完整文件路徑。

path.toRealPath())– 對(duì)于符號(hào)鏈接或解析路徑名中的.或..符號(hào),默認(rèn)跟隨鏈接。

path.getParent()– 獲取路徑的父目錄。

(1)路徑 =/home/mkyong/test/file.txt

Path path = Paths.get("/home/mkyong/test/file.txt");
path                      : /home/mkyong/test/file.txt
path.toAbsolutePath()     : /home/mkyong/test/file.txt
path.getParent()          : /home/mkyong/test
path.toRealPath()         : /home/mkyong/test/file.txt

(2)路徑= file.txt,文件是指當(dāng)前工作目錄+ file.txt。

Path path = Paths.get("file.txt");
path                      : file.txt
path.toAbsolutePath()     : /home/mkyong/projects/core-java/java-io/file.txt
path.getParent()          : null
path.toRealPath()         : /home/mkyong/projects/core-java/java-io/file.txt

(3)Path = /home/mkyong/test/soft-link,一個(gè)符號(hào)鏈接。

$ ln -s
  /home/mkyong/projects/core-java/java-io/src/main/java/com/mkyong/io/howto/GetFilePath.java
  /home/mkyong/test/soft-link
Path path = Paths.get("/home/mkyong/test/soft-link");
path                      : /home/mkyong/test/soft-link
path.toAbsolutePath()     : /home/mkyong/test/soft-link
path.getParent()          : /home/mkyong/test
path.toRealPath()         : /home/mkyong/projects/core-java/java-io/src/main/java/com/mkyong/io/howto/GetFilePath.java

(4)路徑 =/home/mkyong/test/../hello.txt

Path path = Paths.get("/home/mkyong/test/../hello.txt");
path                      : /home/mkyong/test/../hello.txt
path.toAbsolutePath()     : /home/mkyong/test/../hello.txt
path.getParent()          : /home/mkyong/test/..
path.toRealPath()         : /home/mkyong/hello.txt

(5)下面是一個(gè)完整的Java例子來獲取不同的文件路徑Path。

package com.mkyong.io.howto;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
public class GetFilePath1 {
    public static void main(String[] args) {
        // full path
        Path path1 = Paths.get("/home/mkyong/test/file.txt");
        System.out.println("\n[Path] : " + path1);
        printPath(path1);
        // file name
        Path path2 = Paths.get("file.txt");
        System.out.println("\n[Path] : " + path2);
        printPath(path2);
        // soft or symbolic link
        Path path3 = Paths.get("/home/mkyong/test/soft-link");
        System.out.println("\n[Path] : " + path3);
        printPath(path3);
        // a path contains `..`
        Path path4 = Paths.get("/home/mkyong/test/../hello.txt");
        System.out.println("\n[Path] : " + path4);
        printPath(path4);
    }
    static void printPath(Path path) {
        System.out.printf("%-25s : %s%n", "path", path);
        System.out.printf("%-25s : %s%n", "path.toAbsolutePath()",                                                path.toAbsolutePath());
        System.out.printf("%-25s : %s%n", "path.getParent()", path.getParent());
        System.out.printf("%-25s : %s%n", "path.getRoot()", path.getRoot());
        try {
            if (Files.notExists(path)) {
                return;
            }
            // default, follow symbolic link
            System.out.printf("%-25s : %s%n", "path.toRealPath()",                                                  path.toRealPath());
            // no follow symbolic link
            System.out.printf("%-25s : %s%n", "path.toRealPath(nofollow)",
                path.toRealPath(LinkOption.NOFOLLOW_LINKS));
            // alternative to check isSymbolicLink
            /*if (Files.isSymbolicLink(path)) {
                Path link = Files.readSymbolicLink(path);
                System.out.println(link);
            }*/
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

輸出

[Path] : /home/mkyong/test/file.txt
path                      : /home/mkyong/test/file.txt
path.toAbsolutePath()     : /home/mkyong/test/file.txt
path.getParent()          : /home/mkyong/test
path.getRoot()            : /
path.toRealPath()         : /home/mkyong/test/file.txt
path.toRealPath(nofollow) : /home/mkyong/test/file.txt
[Path] : file.txt
path                      : file.txt
path.toAbsolutePath()     : /home/mkyong/projects/core-java/java-io/file.txt
path.getParent()          : null
path.getRoot()            : null
path.toRealPath()         : /home/mkyong/projects/core-java/java-io/file.txt
path.toRealPath(nofollow) : /home/mkyong/projects/core-java/java-io/file.txt
[Path] : /home/mkyong/test/soft-link
path                      : /home/mkyong/test/soft-link
path.toAbsolutePath()     : /home/mkyong/test/soft-link
path.getParent()          : /home/mkyong/test
path.getRoot()            : /
path.toRealPath()         : /home/mkyong/projects/core-java/java-io/src/main/java/com/mkyong/io/howto/GetFilePath.java
path.toRealPath(nofollow) : /home/mkyong/test/soft-link
[Path] : /home/mkyong/test/../hello.txt
path                      : /home/mkyong/test/../hello.txt
path.toAbsolutePath()     : /home/mkyong/test/../hello.txt
path.getParent()          : /home/mkyong/test/..
path.getRoot()            : /
path.toRealPath()         : /home/mkyong/hello.txt
path.toRealPath(nofollow) : /home/mkyong/hello.txt

2.獲取文件的文件路徑(legacy File)

對(duì)于 legacy IO java.io.File,我們可以使用以下 API 來獲取文件的文件路徑。

file.getAbsolutePath()=path.toAbsolutePath()

file.getCanonicalPath()=path.toRealPath()

file.getParent()=path.getParent()

package com.mkyong.io.howto;
import java.io.File;
import java.io.IOException;
public class GetFilePath2 {
    public static void main(String[] args) {
        // full file path
        File file1 = new File("/home/mkyong/test/file.txt");
        System.out.println("[File] : " + file1);
        printFilePath(file1);
        // a file name
        File file2 = new File("file.txt");
        System.out.println("\n[File] : " + file2);
        printFilePath(file2);
        // a soft or symbolic link
        File file3 = new File("/home/mkyong/test/soft-link");
        System.out.println("\n[File] : " + file3);
        printFilePath(file3);
        // a file contain `..`
        File file4 = new File("/home/mkyong/test/../hello.txt");
        System.out.println("\n[File] : " + file4);
        printFilePath(file4);
    }
    // If a single file name, not full path, the file refer to
    // System.getProperty("user.dir") + file
    static void printFilePath(File file) {
        // print File = print file.getPath()
        System.out.printf("%-25s : %s%n", "file.getPath()", file.getPath());
        System.out.printf("%-25s : %s%n", "file.getAbsolutePath()",
                                              file.getAbsolutePath());
        try {
            System.out.printf("%-25s : %s%n", "file.getCanonicalPath()",
                                                file.getCanonicalPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.printf("%-25s : %s%n", "Parent Path", getParentPath(file));
    }
    // if unable to get parent, try substring to get the parent folder.
    private static String getParentPath(File file) {
        if (file.getParent() == null) {
            String absolutePath = file.getAbsolutePath();
            return absolutePath.substring(0,
                          absolutePath.lastIndexOf(File.separator));
        } else {
            return file.getParent();
        }
    }
}

輸出

[File] : /home/mkyong/test/file.txt
file.getPath()            : /home/mkyong/test/file.txt
file.getAbsolutePath()    : /home/mkyong/test/file.txt
file.getCanonicalPath()   : /home/mkyong/test/file.txt
Parent Path               : /home/mkyong/test
[File] : file.txt
file.getPath()            : file.txt
file.getAbsolutePath()    : /home/mkyong/projects/core-java/java-io/file.txt
file.getCanonicalPath()   : /home/mkyong/projects/core-java/java-io/file.txt
Parent Path               : /home/mkyong/projects/core-java/java-io
[File] : /home/mkyong/test/soft-link
file.getPath()            : /home/mkyong/test/soft-link
file.getAbsolutePath()    : /home/mkyong/test/soft-link
file.getCanonicalPath()   : /home/mkyong/projects/core-java/java-io/src/main/java/com/mkyong/io/howto/GetFilePath.java
Parent Path               : /home/mkyong/test
[File] : /home/mkyong/test/../hello.txt
file.getPath()            : /home/mkyong/test/../hello.txt
file.getAbsolutePath()    : /home/mkyong/test/../hello.txt
file.getCanonicalPath()   : /home/mkyong/hello.txt
Parent Path               : /home/mkyong/test/..

 

提交申請(qǐng)后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)

免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 高清性色生活片欧美在线 | 新97人人模人人爽人人喊 | 男人午夜禁片在线观看 | 久久精品国产麻豆不卡 | 在线a人片免费观看国产 | jyzziyzzz免费视频国产 | 一本大道香蕉在线高清视频 | 精品一区二区三区视频在线观看 | 国产一级片观看 | 欧美久久天天综合香蕉伊 | 最近中文免费字幕8 | 在线毛片免费观看 | 成人黄色一级毛片 | 国产高清在线精品一区在线 | 国产精品视_精品国产免费 国产精品视频a | 亚洲国产精品久久网午夜 | 伊人五月| 宠文肉细致高h一对一 | 黄色樱桃试色免费 | 日韩中文字幕免费 | 亚洲天堂免费在线视频 | 亚洲综合激情另类小说区 | 好好的日视频www | 理论片中文 | 欧美成人18 | h小视频在线 | 一区二区三区在线播放 | 手机看片久久高清国产日韩 | 日韩欧美h | 性放荡一级小说 | 亚洲天堂久久久 | 外国xxx | 曰皮全部过程视频免费国产 | 资源在线www天堂 | 一个人看的www片免费高清中文 | 六月丁香综合 | 黄的视频网站 | 日本二级黄色片 | 日韩久久网 | 伊人欧美在线 | 国产成在线观看免费视频成本人 |