• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

one doubt in bufferedReader

 
priya pillai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class PrintFile {
public static void main(String[] args){
bufferedReader buffReader = null;
//more code here to initialize buffReader
try{
String temp;

While(_______________(1) _____________(2)){
System.out.println(temp);
}
}catch _______________(3)
e.printStackTrace();
}
}
}

code Fragements
1.(temp =buffReader.readLine())
2.$$ bufReader.hasNext()
3.(temp =buffReader.nextLine())
4.!=null
5.(IOException e){
6.(FileNotFoundException){
Can anybody help me to sort out.....

thanks a lot
priya
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like sorting out tangled code.



I left major blunders, but it moves ahead of what you put up.

What is the $$ in your original code ?
 
nico dotti
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm thinking that they want you to fill in the blanks so to speak (ie (1) put code there, etc). I don't know if it helps to solve it for you but:
First I think the While somehow got formatted as the W needs to be lower case. So that first line of while is going to try to read a line from the buffered reader: (temp =buffReader.readLine()) and it's in the while loop condition test (the loop is going to keep going until this condition is false...so what condition should we keep looping until? Well, until there aren't any more lines left to read! So: (temp =buffReader.readLine()) !=null which basically reads 'while the next line in the buffer read into our String (temp) does not put null into that String (tmp) execute the following block of code (the while loop). As soon as the readLine() method returns a null the flow will jump to the line just after the while loop (you knew that though wink/wink) That's how the looping is happening. That being said, we'll only get into the loop when the above condition is true (there's an actual line to put into 'temp'). So once we're into the loop the sysout is just printing it to console. The other thing to note is that we're doing all of this within a try catch, because this is an io operation that could throw and IOException. The commented code may throw a FileNotFoundException when initializing the BufferedReader as it may not be able to find the file. However, there are many other IO related problems that may arise (unsupported data type, end of file, etc.), and you should really look at the javadocs to see the IOException's that may arise or read up on that. In any event, IOException is more 'general' than FileNotFoundException, and in fact FNFE extends from IOException. Therefore, if you catch the IOException, you'll be taking care to a possible FileNotFoundException as well as other possible issues. Hence for the (3) section you'd go with IOE...as opposed to the other option FNFE (abbreviations of course). Hope that helps.

code Fragements
1.(temp =buffReader.readLine())
2.$$ bufReader.hasNext()
3.(temp =buffReader.nextLine())
4.!=null
5.(IOException e){
6.(FileNotFoundException){
Can anybody help me to sort out.....
 
nico dotti
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I should also note that you COULD catch the FileNotFoundException and THEN the IOException (in that order because you have to catch from least to most specific...it doesn't make sense to catch IOE first because than you'd never have a FNFE to catch as IOE will already have caught the FNFE! Moreover, this will not even get past the compiler) So it would be:

 
priya pillai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you nico dotti
i was totally confused in this exception

priya
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think your code should go likt this except in your hasNext() method as am not sure that there's hasNext() asssociated with BufferReader.Else it should go like this-:

class PrintFile {
public static void main(String[] args){
BufferedReader buffReader = null;
// more code here to initialize buffReader
File f= new File("D:\\MyFileLLL.txt");



try{
String temp;
//FileReader fw= new FileReader(f);
//buffReader= new BufferedReader(fw);
while((temp =buffReader.readLine())!=null ){
System.out.println(temp);
}
}catch (IOException e){
e.printStackTrace();
}
}
}

thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic