• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

OCP 8. IO File system objects. How to create them?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!

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?



A. None
B. One
C. Two
D. Three

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!
 
Marshal
Posts: 67443
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Javier Gonzalez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Javier, the FileWriter constructor may or may not create the file immediately, it is system dependent. The call to flush() will definitely create the file.
 
Javier Gonzalez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ankit, I got it now.
 
Javier Gonzalez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to Campbell too!
 
Campbell Ritchie
Marshal
Posts: 67443
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Javier Gonzalez wrote:Thank you  . . .

On behalf of everybody who replied, “That's a pleasure.”
 
Self destruct mode activated. Instructions for deactivation encoded in this tiny ad.
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!