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 ]
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
"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
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? =)
Oscar S. Garcia<br />SCJP<p>--------------------------<br />Pilipino ako...ayos ba?<br />--------------------------
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
Don't get me started about those stupid light bulbs.