Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IO Questions

 
Salamina Daniel
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are there any difference between these two:

------------------
/jan
[This message has been edited by Jan Andersson (edited November 19, 2001).]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic