Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty File constructor is neither file nor directory

Tags:

java

file

file-io

What is the difference between the following two methods for creating a file?

new File(System.getProperty("user.dir"));
new File("");

Java identifies the first one as a directory, and the second one's neither a file nor a directory! Why is that the case?


Code:

public class MainClass {
    public static void main(String[] args) throws Exception {       
        System.out.println("File Created with CurrentDir taken From System Props");
        File f1 = new File(System.getProperty("user.dir"));
        System.out.println("Absolute Path: " + f1.getAbsolutePath());
        System.out.println("isDirectory: " + f1.isDirectory());
        System.out.println("isFile: " + f1.isFile());

        System.out.println();

        System.out.println("File Created with Empty String Path");
        File f2 = new File("");
        System.out.println("Absolute Path: " + f2.getAbsolutePath());
        System.out.println("isdirectory: " + f2.isDirectory());
        System.out.println("isFile: " + f2.isFile());       
    }
}

Output:

File Created with CurrentDir taken From System Props
Absolute Path: D:\Java Workspace\my_Workspace\JavaTest
isDirectory: true
isFile: false

File Created with Empty String Path
Absolute Path: D:\Java Workspace\my_Workspace\JavaTest
isdirectory: false
isFile: false
like image 426
Amal Avatar asked Jun 12 '18 09:06

Amal


People also ask

Does file constructor create file?

The File() constructor creates a new File object instance.

Does Java new file create file?

File class can be used to create a new File in Java. When we initialize File object, we provide the file name and then we can call createNewFile() method to create new file in Java. File createNewFile() method returns true if new file is created and false if file already exists. This method also throws java.

Does File New File Create a new file?

The createNewFile() function is a part of File class in Java . This function creates new empty file. The function returns true if the abstract file path does not exist and a new file is created. It returns false if the filename already exists.

How do you create a directory if it does not exist in Java?

You can use the Java File class to create directories if they don't already exists. The File class contains the method mkdir() and mkdirs() for that purpose. The mkdir() method creates a single directory if it does not already exist.


2 Answers

Explanation

It may seem a little non-intuitive but actually that's just how the class is supposed to work according to its documentation. It's called empty abstract pathname in the documentation:

The empty abstract pathname has no prefix and an empty name sequence.

And from your constructor File#File(String):

Creates a new File instance by converting the given pathname string into an abstract pathname. If the given string is the empty string, then the result is the empty abstract pathname.

So the File class actually interprets the empty name as actual name. When you test File#isDirectory() or File#isFile() it thus checks if there exists a file or directory like

D:\Java Workspace\iTAW_Workspace\JavaTest\<empty>

Note the <empty> which I wrote to indicate that it actually searches for a file here with the empty name. Obviously such a file can not exist, thus the result will always be false. So again, it does not check

D:\Java Workspace\iTAW_Workspace\JavaTest\

but rather the empty file in this directory, which does not exist.

Unfortunately you don't see this when using the File#toAbsolutePath() method as there is no representation for an empty name.


NIO

Note that the class File and everything related to it is outdated. Nowadays file IO is done using NIO revolving around Files, Paths and Path. This API is much more cleaner and more intuitive. It will also work as intended on your current example:

Files.isDirectory(Paths.get("")); // true

Take a look at the documentation for more.

like image 163
Zabuzard Avatar answered Oct 16 '22 08:10

Zabuzard


Creating a file with empty string results to creating a File instance which actually does not exist and its absolute pathname is "empty abstract pathname".

-> That's why the second one's neither a file nor a directory for you.

The reason behind that, a maybe little bit confusing output for you, is definition located in javadocs:

If this abstract pathname is the empty abstract pathname then the pathname string of the current user directory, which is named by the system property user.dir, is returned.

You can find more about this topic here https://docs.oracle.com/javase/6/docs/api/java/io/File.html

like image 23
dontlookatme Avatar answered Oct 16 '22 09:10

dontlookatme