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?
Advertisement
Answer
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.