I have a doubt about the Practice Test book (Boyarsky/Selikoff): Chapter 18 Question 32.
32. Assuming the working directory is accessible, empty, and able to be written, how many file
system objects does the following class create?
The answer is C. Two.
And the explanation is :
C. Line 5 creates a File object, but that does not create a file in the file system unless
cake.createNewFile() is called. Line 6 also does not necessarily create a file, although
the call to flush() will on line 7. Note that this class does not properly close the file
resource, potentially leading to a resource leak. Line 8 creates a new File object, which is
used to create a new directory using the mkdirs() method. Recall from your studies that
mkdirs() is similar to mkdir(), except that it creates any missing directories in the path.
Since directories can have periods (.) in their name, such as a directory called info.txt,
this code compiles and runs without issue. Since two file system objects, a file and a directory, are created, Option C is the correct answer.
This means that when we call the method mkdirs() is creating 2 file system objects in just one operation? The file system object "fudge.txt" (instance File) and the directory "fudge.txt"
I am a little bit confuse...
Thank you in advance!
If you look at the File#mkdirs() method, you will find it creates a directory; it doesn't say anything about creating a file in that directory. Remember that new File("fudge.txt") oesn't create a file in the file system.
posted 4 weeks ago
So the file system object is created on this line:
Writer pie = new FileWriter("pie.txt");
I was confused because when I run before this code on my laptop it created the file.
But the explanation of the answer it was a little bit confuse:
"Line 6 also does not necessarily create a file, although
the call to flush() will on line 7"