Does the File (String) constructor create a file?

6

When we use the constructor File f = new File(aquiVemOEnderecoDoArquivo) method, if the file does not exist, is it created?

    
asked by Isdeniel 04.12.2018 в 14:04

3 answers

7

No, the constructor of the File class creates a representative instance of the path that you inform the file or directory, but nothing is created in the File System by the constructor, as can be seen in the next , removed from the own documentation :

  

Instances of this class may or may not denote an existing file-system object such as a file or a directory. If it does denote such an object then that object resides in a partition. A partition is an operating system-specific portion of storage for a file system. A single storage device (eg a physical disk-drive, flash memory, CD-ROM) may contain multiple partitions. The object, if any, will reside on the partition named by some ancestor of the absolute form of this pathname.

Free translation:

  

Instances of this class may or may not denote an actual file system object, such as a file or directory. If it denotes such an object, then that object resides in a partition. A partition is a specific part of the storage operating system for a file system. A single storage device (for example, a physical disk drive, flash memory, CD-ROM) may contain multiple partitions. The object, if any, will reside on the partition named by some ancestor of the absolute form of that pathname.

If you want to check in practice, you can run the code below, which will create an instance of a text file that does not exist in your user folder. First check the existence, then create the file and check again. You can follow up with your open user folder.

import java.io.File;
import java.io.IOException;

public class FileClassTest {

    public static void main(String[] args) throws IOException {

        String userDir = System.getProperty("user.home");

        File  file = new File(userDir + System.getProperty("file.separator") + "someUnknownFile.txt");

        System.out.println(file.exists());

        file.createNewFile();

        System.out.println(file.exists());

    }
}

Remembering that the code will only return false the first time it is executed, because when you run file.createNewFile(); , the file will be created in the File System.

    
04.12.2018 / 14:25
1

Do not create.

The documentation says the following:

  

File (String pathname)

     

Creates a new File instance by converting the given pathname string into an abstract pathname.

As the text informs, an abstract path is created, that is, physically non-existent. The file itself is actually created only when you use methods that write (or prepare the writing) effectively on the disk. The code below lets you prove this:

File f = new File("teste.txt");
System.out.println(f.exists()); //false
OutputStream anOutputStream = new FileOutputStream(f.getName());
System.out.println(f.exists()); //true
    
04.12.2018 в 14:34
0

Based on the comments in the post and in a few minutes I thought about it, I came to the conclusion that the File(String path) counter does not create a file. I can say this because if the constructor created the file, the existence of the function exists() and createNewFile() would be useless.

    
04.12.2018 в 14:21