GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Boone's exam: FileOutputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Boone Watch "Boone New topic
Author

Boone's exam: FileOutputStream

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

Question 38: What does the following code do?
File f = new File("hello.test");

FileOutputStream out = new FileOutputStream(f);
Select the one right answer.

1. Create a new file named "hello.test" if it does not yet exist. It also opens the file so you can write to it.
2. Create a new file named "hello.test" if it does not yet exist. The file is not opened.
3. Open a file named "hello.test" so that you can write to it and read from it, but does not create the file if it does not yet exist.
4. Open a file named "hello.test" so that you can write to it but cannot read from it.
5. Create an object that you can now use to create and open the file named "hello.test," and write to and read from the file.


Given answer : Answer 1

I find "4" also correct.


Answer explanation is given as :
Question 38: a. The first line creates a File object that represents the file. By creating a FileOutputStream, you create the file if it does not yet exist, and open that file for reading and writing. (See chapter 11.)



Please comment on this!


Regards,
cmbhatt


cmbhatt
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I disagree with the statement about the FileOutputStream opening the file for reading and writing. A FileOutputStream is meant for writing.
Jesse Custer
Ranch Hand

Joined: Feb 07, 2007
Posts: 45
Indeed, using the FileOutputStream you can only write to the file. However, a file opened by a FileOutputStream, can still be used simultaneously by a FileInputStream to write data to the file. So it is possible to read and write to the file.

The reason why answer 4 is probably not correct is that it doesn't state it will create the file if it doesn't exist.
swarna dasa
Ranch Hand

Joined: Mar 15, 2007
Posts: 108
API Docs definition of FileOutputStream

A file output stream is an output stream for writing data to a File or to a FileDescriptor. Whether or not a file is available or may be created depends upon the underlying platform.

So does this mean that the creation of a file is platform dependent? In that case, we can not assume that the file is created?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I think in a question like this, it will probably be stated to choose the best answer.

It is certainly not guaranteed that the file will be created or opened.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Swarna,

Your API definition caused one more confusion of platform dependency;

What is opening a file for writing;
doesn't FileOutputStream open file too besides creating;

This "opening" word has confused me several times while I was giving mock!!!





Regards,
cmbhatt
Omer Haderi
Ranch Hand

Joined: Sep 27, 2006
Posts: 42
Hey,

Sorry for disturbing the flow of this thread, just a clarification:

Is FileOutputStream on th SCJP 5 ?

In the K&B book and the objectives lists only the following:
File
FileReader
FileWriter
BufferedReader
BufferedWriter
PrintWriter

ObjectInputStream
ObjectOutputStream

just to make sure...
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Omer,

Refer to synchronization part; Aren't FileOutputStream and FileInputStream of
java.io required along with ObjectOutputStream and ObjectInputStream to serialize and deserialize the instances?

How to write streams physically on the disk?

Finally, They are on the exam!


Regards,
cmbhatt
[ April 17, 2007: Message edited by: Chandra Bhatt ]
megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
I also recall a statement in the bold notes box saying, anytime you encounter "Stream" word , it means you only have to worry about synchronization objectives and not IO objectives. This question is more pertaining to IO objective, but its always good to know more.
 
GeeCON Prague 2014
 
subject: Boone's exam: FileOutputStream