• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

i/o streams

 
ashok khetan
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
what will be right answer for the following question?(from boone's mock-exam).
File f = new File("hello.test");
FileOutputStream out = new FileOutputStream(f);

Select the one right answer.
a) Create a new file named "hello.test" if it does not yet exist. It also opens the file so you can write to it and read from it.
b) Create a new file named "hello.test" if it does not yet exist. The file is not opened.
c) 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.
d) Open a file named "hello.test" so that you can write to it but cannot read from it.
e) 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.
 
Roopa Bagur
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the answer is b
Originally posted by ashok khetan:
hi,
what will be right answer for the following question?(from boone's mock-exam).
File f = new File("hello.test");
FileOutputStream out = new FileOutputStream(f);

Select the one right answer.
a) Create a new file named "hello.test" if it does not yet exist. It also opens the file so you can write to it and read from it.
b) Create a new file named "hello.test" if it does not yet exist. The file is not opened.
c) 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.
d) Open a file named "hello.test" so that you can write to it but cannot read from it.
e) 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.

 
Mini Pilla
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the answer is d.
Actually u can do a search of these type of questions which are ambigious.
Thanx
Rajani

[This message has been edited by Rajani Katta (edited August 31, 2001).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the answer is b
 
swati bannore
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
Even I had posted this question recently.I think,option d is correct.When we create a FileOutputStream Object,a file object is created for writing to the file or if it exists,it is opened for writing.
It thorws exception in following cases-
If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.

 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File f = new File("hello.test")
does not create a file.
File (from the Java Tutorial) "represents a file on the native file system. You can create a File object for a file on the native file system."
Therefore 'b' cannot be the correct answer.
[This message has been edited by Marilyn deQueiroz (edited September 01, 2001).]
 
Abdul Latif
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the answre is d
Because a FileOutputStream creates a file if the file object does not represent a file on the host macnine.
since FileOutputStream is used for writing to files it can only wite & cant read
hope it helps
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ashok,
The API doc says :


public FileOutputStream(File file) throws FileNotFoundException
Creates a file output stream to write to the file represented by the specified File object.



The file is created using java.io.FileOuptputStream.But is the file opened?I think the answer is YES, otherwise you cannot write into it.Hence, D is the correct answer.
B is incorrect, since the API DOC also mentions that the parameter to the Constructor, is the File to be opened for writing.
Hope this helps,
Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic