wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes IO Questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "IO Questions" Watch "IO Questions" New topic
Author

IO Questions

Salamina Daniel
Ranch Hand

Joined: Oct 10, 2001
Posts: 41
A file is going to be read one line at a time into String objects. Which construction is most suitable for reading the file ?
1. FileInputStream in=new FileInputStream("file.name");
2. DataInputStream in=new DataInputStream(new FileInputStream("file.name"));
3. DataInputStream in= new DataInputStream(new FileInputStream("file.name","r"));
4. BufferedReader in=new BufferedReader(new InputStreamReader(new FileInputStream("file.name")));
5. BufferedReader in=new BufferedReader(new InputStreamReader(new FileInputStream("file.name"),"8859_1"));
I believe the answer should be (2.) as DataInputStream has a readUTF()method that returns a String.
Answer 1 - wrong because FileInputStream reads bytes (low-level input stream)
Answer 3 - wrong construction- related to RandomAccessFile(File file, String mode) not FileInputStream
Answer 4 - wrong - BufferedReader read data as chars (StringReader read data as Strings)
Answer 5 - wrong - same as 4 but specify an encoding construction
But in the answer it is mentioned that we shoul read one line at a time so I think we can do the job with a LineNumberReader -readLine() return String so the answer 4 could be correct if we chain like this:
BufferedReader in=new BufferedReader(new InputStreamReader(new FileInputStream("file.name")));
LineNumberReader l= new LineNumberReader(in);
So what is the correct answer ?
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Originally posted by Salamina Daniel:
A file is going to be read one line at a time into String objects. Which construction is most suitable for reading the file ?
1. FileInputStream in=new FileInputStream("file.name");
2. DataInputStream in=new DataInputStream(new FileInputStream("file.name"));
3. DataInputStream in= new DataInputStream(new FileInputStream("file.name","r"));
4. BufferedReader in=new BufferedReader(new InputStreamReader(new FileInputStream("file.name")));
5. BufferedReader in=new BufferedReader(new InputStreamReader(new FileInputStream("file.name"),"8859_1"));
I believe the answer should be (2.) as DataInputStream has a readUTF()method that returns a String.
Answer 1 - wrong because FileInputStream reads bytes (low-level input stream)
Answer 3 - wrong construction- related to RandomAccessFile(File file, String mode) not FileInputStream
Answer 4 - wrong - BufferedReader read data as chars (StringReader read data as Strings)
Answer 5 - wrong - same as 4 but specify an encoding construction
But in the answer it is mentioned that we shoul read one line at a time so I think we can do the job with a LineNumberReader -readLine() return String so the answer 4 could be correct if we chain like this:
BufferedReader in=new BufferedReader(new InputStreamReader(new FileInputStream("file.name")));
LineNumberReader l= new LineNumberReader(in);
So what is the correct answer ?


Well.. Based on the available choices. 2 will be appropriate
But linenumberreader may help to readline() as a single string object...
Ragu
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Hi guys,
I'd just like to remember you one thing, the questions says "A file is going to be read one line at a time..." So the only correct answer can be either 4 or 5 since readUTF of DataInputStream only returns a String object and not a whole line. BufferedReader has a readLine method which can achieve what is asked. Now, we have to choose between 4 and 5 and it's not very difficult since the encoding 8859_1 does not exist (ISO-8859-1 does though)
So the correct answer should be 4.
Note: LineNumberReader is a subclass of BufferedReader... The solution you gave is correct but not necessary since BufferedReader already provides all constructs necessary to fulfill what is asked...
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited November 16, 2001).]


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Originally posted by Valentin Crettaz:
Hi guys,
I'd just like to remember you one thing, the questions says "A file is going to be read [b]one line at a time
..." So the only correct answer can be either 4 or 5 since readUTF of DataInputStream only returns a String object and not a whole line. BufferedReader has a readLine method which can achieve what is asked. Now, we have to choose between 4 and 5 and it's not very difficult since the encoding 8859_1 does not exist (ISO-8859-1 does though)
So the correct answer should be 4.
Note: LineNumberReader is a subclass of BufferedReader... The solution you gave is correct but not necessary since BufferedReader already provides all constructs necessary to fulfill what is asked...
HIH
[/B]

Thankx Val...
Good to see your msg once in a while...
Here is my thought.
My choice is 2 is not based readUTF()... method
But becoz of readLine() method... Since this method is implemented from DataInput interface...
But the preferred way is what you had mentioned choice 4
since readLine() is deprecated....
So I wont agree totally and rule out that choice 2 is wrong... just that it may not be preferred way of doing it

Thankyou
Ragu
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Hi Ragu,
Again the question says "Which construction is most suitable for reading the file ?"
As you pointed out readLine in DataInputStream is deprecated and there is a note in the API stating that a preferred way is to use BufferedReader.readLine().
Again, the most suitable choice here in this very question is 4 and not 2, do you agree ? I mean there is no other choice if you want to fully comply with the wording of the question.
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Originally posted by Valentin Crettaz:
Hi Ragu,
Again the question says "Which construction is [b]most suitable
for reading the file ?"
As you pointed out readLine in DataInputStream is deprecated and there is a note in the API stating that a preferred way is to use BufferedReader.readLine().
Again, the most suitable choice here in this very question is 4 and not 2, do you agree ? I mean there is no other choice if you want to fully comply with the wording of the question.
[/B]

"most suitable"
Key point... Thankx Val
As long as your are here in javaranch, I always feel I will
get a correct response :-)
Have fun
Ragu
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Originally posted by Ragu Sivaraman:
As long as your are here in javaranch, I always feel I will
get a correct response :-)
Have fun
Ragu

What a compliment ! Thanx
You have fun too !

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
Salamina Daniel
Ranch Hand

Joined: Oct 10, 2001
Posts: 41
Thanks a lot for your answers. Really helped me clarify my mind. Indeed BufferedReader has a readLine() method that will do the job.
Best regards
Jan Andersson
Greenhorn

Joined: Nov 19, 2001
Posts: 5
Are there any difference between these two:

------------------
/jan
[This message has been edited by Jan Andersson (edited November 19, 2001).]


/jan
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
with the first you can specify an encoding to usr when reading the stream while with the second the encoding defaults to the platform encoding.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited November 19, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: IO Questions