Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse package explorer vs project explorer view 2 135
servlet example 11 40
hibernate example for saving data 19 43
How to determine if a string is a valid SHA value 7 34
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)
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

831 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