• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Tutorial I/O question  RSS feed

 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 5. How can you improve the performance of the following code? Explain your answer and show the new line(s) of code.
int i;
URL url = new URL("http://java.sun.com/");
URLConnection javaSite = url.openConnection();
InputStream input = javaSite.getInputStream();
InputStreamReader reader = new InputStreamReader(input);
while ((i = reader.read()) != -1) {
System.out.print(i);

Why in the answer they utilize two Buffered objects instead of just one?
int i;
URL url = new URL("http://java.sun.com/");
URLConnection javaSite = url.openConnection();
InputStream input = javaSite.getInputStream();
BufferedInputStream in = new BufferedInputStream(input);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while ((i = reader.read()) != -1) {
System.out.print(i);
}
 
Kem Bro
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I'm not 100% on this as I'm new to this game.
To start off, buffering is more efficient.
I think that BufferedInputStream is used to read in bytes,
and then this byte stream is used in the constructor for
the BufferedReader object, converting the byte stream to
a character stream.
Is this right? I'd appreciate it if someone would clear
this one up for me too
 
prasad bhn
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi rerun,
you are 100% correct!
- prasadbh.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!