1

I've read Oracle documentation, but I still don't get it. For example:

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

and

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

Are they the same in this case?

Viktor
  • 566
  • 5
  • 17
  • Does this answer your question? [Difference between mkdir() and mkdirs() in java for java.io.File](https://stackoverflow.com/questions/9820088/difference-between-mkdir-and-mkdirs-in-java-for-java-io-file) – Ivan Barayev Jan 30 '21 at 14:43
  • 1
    @IvanBarayev no. I know what mkdir() and mkdirs() are. I want to know what is the difference between two constructors of java.io.File(). – Viktor Jan 30 '21 at 14:45

1 Answers1

3

So taken from the OpenJDK 8(https://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/io/File.java) the code the the one String Constructor is:

public File(String pathname) {

    if (pathname == null) {

        throw new NullPointerException();

    }

    this.path = fs.normalize(pathname);

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

}

and for the two String Constructor is:

 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);

    }

where normalize just removes stuff like . and .. So you may see that parent and child do an additional call to the file system. But the call only means "Give me the path to child if the path is realative to parent". So in your call they will result in the same behavior.

jadnb
  • 91
  • 4