Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Boone's exam: FileOutputStream

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic