When we use the constructor File f = new File(aquiVemOEnderecoDoArquivo)
method, if the file does not exist, is it created?
When we use the constructor File f = new File(aquiVemOEnderecoDoArquivo)
method, if the file does not exist, is it created?
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.
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
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.