• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JavaCaps Test 2, Question # 34

 
Tina Ang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which line of the program could be throwing an exception, if the program is as listed below. Assume that "MyFile2.txt" is a read only file.

Note: MyFile2.txt is read only file..

A) This program never throws IOException
B) The line f1.close() throws IOException
C) While writing to the stream f1.write(buffer[i]) throws an IOException
D) While constructing the FileWriter f2 = new FileWriter("MyFile2.txt");

Answer is D. Could someone please explain why this is the answer.
Thank you!

TINA
----------
Edited code to fit page better
-Jess
[ April 23, 2002: Message edited by: Jessica Sant ]
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FileWriter is subclass of FileOutputStream and when FileOutputstraem construtor is created then

1) If the file does not exist, it is created.
2) If is exists, its contents are reset, unless the appro. construtor is used to tell that o/p should be appended to the file.
3) A SecurityException is thrown if file does not have write access or can not be created

source - Khalid
HTH
 
Oscar Garcia
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi tina!
Note that it was assumed that the given file is
a "read-only" file. So, it will fail when trying to write to that file...remember, it is not writable...
hope this helps...ayos ba? =)
 
Steven Wong
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
from Ravish
FileWriter is subclass of FileOutputStream and when FileOutputstraem construtor is created then

FileWriter is a subclass of OutputStreamWriter, not FileOutputStream.
Clement
 
Gautam Sewani
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the MyFile2.txt is read only,constructing a file writer to write on that file will cause a security exception to be thrown.Hence the answer d is correct.
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic