Solved

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

Posted on 2013-11-02
5
338 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 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
Configure a Bean in an XML file 4 49
hibernate jars 4 61
hibernate example for saving data 19 78
Java 8 to Java 6 8 33
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

730 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