簡體   English   中英

Do java.io.File(String parent, String child) and java.io.File(String pathname) do the same work?

[英]Do java.io.File(String parent, String child) and java.io.File(String pathname) do the same work?

我已閱讀Oracle 文檔,但我仍然不明白。 例如:

File("/storage/emulated/0", "directory").mkdirs()

File("/storage/emulated/0/directory").mkdirs()

在這種情況下它們是否相同?

所以取自OpenJDK 8( https://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/io/File.Z93F725A07423FE1C88or9F448B33D21F )構造字符串代碼是:

public File(String pathname) {

    if (pathname == null) {

        throw new NullPointerException();

    }

    this.path = fs.normalize(pathname);

    this.prefixLength = fs.prefixLength(this.path);

}

而對於這兩個字符串構造函數是:

 public File(String parent, String child) {

        if (child == null) {

            throw new NullPointerException();

        }

        if (parent != null) {

            if (parent.equals("")) {

                this.path = fs.resolve(fs.getDefaultParent(),

                                       fs.normalize(child));

            } else {

                this.path = fs.resolve(fs.normalize(parent),

                                       fs.normalize(child));

            }

        } else {

            this.path = fs.normalize(child);

        }

        this.prefixLength = fs.prefixLength(this.path);

    }

其中 normalize 只是刪除類似的東西。 和.. 所以你可能會看到父子節點對文件系統進行了額外的調用。 但是這個電話只意味着“如果路徑對父母來說是真實的,請給我通往孩子的路徑”。 因此,在您的通話中,它們將導致相同的行為。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM