Solved

Java BufferedReader; How do I stop looping through my file?

Posted on 2013-11-02
5
333 Views
Last Modified: 2013-11-02
I'm trying to read in lines of a text file using BufferedReader in Java.  For the sake of this example I would like to simply print those lines to the console.  I have a text file with three lines of text, i.e.:

aaa
bbb
ccc

however, my code below keeps looping through the file such that output is:
aaa
bbb
ccc
aaa
bbb
ccc
...ad infinitum

			try{
				FileReader fr = new FileReader(myFile);
				BufferedReader br = new BufferedReader(fr);
				String strLine;

				System.out.println("Number of lines is: " + numLines(myFile));

				for (int i = 0; i < numLines(myFile); i++){	


					strLine = br.readLine();
					packet = strLine;
					System.out.println(strLine);
					}
				br.close();

			}catch (Exception e){//Catch exception if any
				System.err.println("Error: " + e.getMessage());
			}//end of try/catch

Open in new window




Eventually I have more fun and exciting things to do with the string I read in, but that goes beyond the scope of my question.
0
Comment
Question by:Geisrud
  • 3
  • 2
5 Comments
 
LVL 14

Author Comment

by:Geisrud
ID: 39618902
I've also tried the following with similar results:
			FileInputStream fstream = new FileInputStream(y);	
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			
				while ((strLine = br.readLine()) != null){

					System.out.println(strLine);
				}
			br.close();

Open in new window

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 39619026
This is the pattern you need to use

try {
....
	while((strLine = br.readLine()) != null) {
		System.out.println(strLine);
	}
}
catch(IOException e) {
	e.printStackTrace();
}
finally {
	try { br.close(); } catch(Exception e) { /* ignore */ }
}

Open in new window

0
 
LVL 14

Author Comment

by:Geisrud
ID: 39619094
It seems I was doing this correctly then (my second post, anyway).  However I'm still getting the looping infinitely through my file.  I must have something else going on in my code that is causing that.
0
 
LVL 14

Author Comment

by:Geisrud
ID: 39619098
Ah found it.  Seems I had another while loop surrounding this section that never exited.

Thanks for your help.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39619136
:)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now