Win a copy of Head First Android this week in the Android forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

JavaCaps Test 2, Question # 34

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 5397
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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? =)
 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic