aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Various IO methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Various IO methods" Watch "Various IO methods" New topic
Author

Various IO methods

Vinod Kumar
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
Considering these questions,how one can make sure he is making a correct choice from the available answers for the scenario asked?
Questions:
What is the most appropriate method for read a line from a file and to store it in a String object?
I have a socket handle s. I want to read data a line at a time from the remote network using this socket handle. what is the most appropriate choice
I can use?
Regards,
Vinod
Peter Bugla
Greenhorn

Joined: Feb 12, 2002
Posts: 22
Originally posted by Vinod Pai:

Questions:
What is the most appropriate method for read a line from a file and to store it in a String object?
I have a socket handle s. I want to read data a line at a time from the remote network using this socket handle. what is the most appropriate choice
I can use?

File and socket, doesn't match, so the question doesn't make sense. If we leave out "File", you can try thinking it through like this:
First thing, look at what you have.
That's a Socket here. The only usable thing it offers is an InputStream:
s.getInputStream()
If you want to read data "a line at a time" we're
talking about characters, so it would be nice to use some XxxxReader. This Reader should be able to work with what we have (InputStream), so why not have a look at InputStreamReader:
new InputStreamReader(s.getInputStream())
We want to read "a line" (at a time), probably more than one altogether.
We can of course write some loop reading character and check for some line-ending characters, but perhaps we can avoid this by using some Buffering stuff ... BufferedReader fortunately offers a method readLine giving back a String :-)
BufferedReader b = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = b.readLine();
� voil� :-)


Peter Bugla<br />Sun Certified Programmer for Java 2 Platform
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Good explanation Peter


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Various IO methods