6
When we use the constructor method File f = new File(aquiVemOEnderecoDoArquivo)
, if the file does not exist, it is created?
6
When we use the constructor method File f = new File(aquiVemOEnderecoDoArquivo)
, if the file does not exist, it is created?
7
No, the class builder File
creates a representative instance of the path you enter from the file or directory, but nothing is created in the File System by the builder, as can be seen in the griffin below, taken from the own documentation:
Instances of this class may or may not denote an actual 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. The single Storage device (e.g. 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.
In free translation:
Instances of this class may or may not denote a real file system object, such as a file or a 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 (e.g., a physical disk drive, flash memory, CD-ROM) can contain multiple partitions. The object, if any, will reside in the partition named by some ancestor of the absolute form of that path name.
If you want to check in practice, you can run the code below, which will create an instance of a non-existent text file in your user folder. First you will check the existence, then you will 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 executing file.createNewFile();
, the file will be created in File System.
1
Does 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 nonexistent. The file itself is actually created only when you use methods that write (or prepare writing) effectively on the disk. The code below allows you to 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
0
Based on the comments of the publication and on a few minutes I spent thinking about it, I came to the conclusion that the constructor File(String path)
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.
Browser other questions tagged java
You are not signed in. Login or sign up in order to post.
@renanzin is sure of this?
An abstract representation of file and directory pathnames.
– user28595
not @Articunol ;) deleted here.
– renanvm
Did you ever run the code and see if it creates the file? Anyway, the file is not created, so much so that there is the method
exists
to check if the file actually exists, and the methodcreateNewFile
to create it (and there is alsoFileOutputStream
andFileWriter
, who write to the file - and create, if not available). But only donew File
creates nothing– hkotsubo
I honestly find the negatives my unfair in this case, the documentation is not so clear on this, she does not say explicitly. The doubt is valid.
– user28595
@Articunol I think too
– renanvm
https://stackoverflow.com/questions/44523347/java-new-file-does-not-create-file
– hkotsubo
Now I understand the question, but I think not because look at this excerpt from the documentation of File(string):
"If the given string is the empty string, then the result is the empty abstract pathname."
– Victor Laio