File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes IO Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "IO Question" Watch "IO Question" New topic
Author

IO Question

Suren Babu
Greenhorn

Joined: Oct 23, 2000
Posts: 27
This question is from http://www.javacaps.com/sjpc_mockexam.html.
I checked A and B, but the answers are A,B,E.
As the question said it is 8-bit file and InputStreamReader takes care of converting bytes to character i checked A and B. But i doudt Answer E as it purely deals with Character IO. Could some one explain why Answer E is correct.
Here is the question...
55. You have an 8-bit file using the character set defined by ISO 8859-8. You are writing an application to display this file in a TextArea. The local encoding is already set to 8859-8. How can you write a chunk of code to read the first line from this file?
You have three variables accessible to you:
myfile is the name of the file you want to read
stream is an InputStream object associated with this file
s is a String object
Select all valid answers.
A)
InputStreamReader reader = new InputStreamReader(stream, "8859-8");
BufferedReader buffer = new BufferedReader(reader);
s = buffer.readLine();
B)
InputStreamReader reader = new InputStreamReader(stream);
BufferedReader buffer = new BufferedReader(reader);
s = buffer.readLine();
C)
InputStreamReader reader = new InputStreamReader(myfile, "8859-8");
BufferedReader buffer = new BufferedReader(reader);
s = buffer.readLine();
D)
InputStreamReader reader = new InputStreamReader(myfile);
BufferedReader buffer = new BufferedReader(reader);
s = buffer.readLine();
E)
FileReader reader = new FileReader(myfile);
BufferedReader buffer = new BufferedReader(reader);
s = buffer.readLine();
Thanks in advance
Panagiotis Varlagas
Ranch Hand

Joined: Nov 27, 2000
Posts: 233
Suren,
Here is why answer E) is correct.
I quote from "Java in a Nutshell" (2nd edition), p. 411:

"FileReader is a convenience subclass of InputStreamReader..."

"The FileReader constructor internally creates a FileInputStream to read bytes from the specified file, and uses the functionality of its superclass, InputStreamReader, to convert those bytes from characters in the local encoding to Unicode characters using Java."

"If you want to read Unicode character from a file that uses some encoding other that the default encoding for the locale, you must explicitly create your own InputStreamReader to perform the byte-to-character conversion".
Of course you can always read the freely-available API.
Hope this helps,
Panagiotis.
---------------------------------
E)
FileReader reader = new FileReader(myfile);
BufferedReader buffer = new BufferedReader(reader);
s = buffer.readLine();
sajida kal
Ranch Hand

Joined: Mar 22, 2001
Posts: 89
Hi
I chanced upon this post while studying IO(one of the most confusing topics with a zillion things to grasp ).
I have a question here. None of the correct options mentioned in the post use a try catch block while calling the readLine method
of the BufferedReader class and all of them would give a compilation error. IMHO, the question should be appropriately worded-
ie it should mention explicitly that the method calling the
correct piece of code throws IOException OR
The code fragments should be revised to trap the exception.
Does anyone agree/disagree ?
Thanks,
Sajida
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: IO Question