Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-11-02
5
Medium Priority
?
353 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

636 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