Solved

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

Posted on 2013-11-02
5
337 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web services creation SOAP vs REST 5 57
even odd program using while loop 3 60
Java: anonymous class 4 29
restrict decimal places for double datatype 10 22
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

828 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