user2526311
user2526311

Reputation: 1004

Reading a .txt file

So I have a class and what it does is read the contents of a file and then does something based on the contents.

package maple;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JOptionPane;

public class SetSettings {

public SetSettings() {

    BufferedReader reader;

    File f = new File (Shared.saveLocation + "Settings.txt");

    String line = "";

    if (f.exists()) {
        while (line != null) {
            try {
                //System.out.println("COMES HERE");
                reader = new BufferedReader(new FileReader(Shared.saveLocation + "Settings.txt"));
                line = reader.readLine();
                System.out.println(line);

            } catch (IOException e2) {
                // TODO Auto-generated catch block
                //e2.printStackTrace();
                //JOptionPane.showMessageDialog(null, "Error Code: Bx001", "Error", 0);

            }
        }
    }
}
 }

For testing purposes I had it print out each line of the .txt. Instead of printing out what is in the .txt file it'll print out the first line on and on. Regardless if there is one or 100 lines in the .txt file it'll just constantly read one line. How do I make it read the next and stop when there are no more?

Upvotes: 3

Views: 288

Answers (6)

Chaos
Chaos

Reputation: 11721

The problem is with this line in your while loop:

reader = new BufferedReader(new FileReader(Shared.saveLocation + "Settings.txt"));

You are creating a new BufferedReader every time, so you're reading the first line over and over again. Move this line outside the loop and try again.

I think you also have to expand your try-catch block to catch the IO exception.

try {
reader = new BufferedReader(new FileReader(Shared.saveLocation + "Settings.txt"));
line = reader.readLine();
while (line != null) {

            //System.out.println("COMES HERE");
            System.out.println(line);
            line = reader.readLine();
           }

} catch (IOException e2) {
            // TODO Auto-generated catch block
            //e2.printStackTrace();
            //JOptionPane.showMessageDialog(null, "Error Code: Bx001", "Error", 0);

             }

Upvotes: 1

jh314
jh314

Reputation: 27802

You should have this:

if (f.exists()) {
    reader = new BufferedReader(new FileReader(Shared.saveLocation + "Settings.txt"));
    while (line != null) {
        try {
            //System.out.println("COMES HERE");

            line = reader.readLine();
            System.out.println(line);

        } catch (IOException e2) {
            // TODO Auto-generated catch block
            //e2.printStackTrace();
            //JOptionPane.showMessageDialog(null, "Error Code: Bx001", "Error", 0);

        }
    }
}

Upvotes: 1

Quillion
Quillion

Reputation: 6476

you keep recreating your buffereReader in the while loop. Take it outside.

if (f.exists()) {
        reader = new BufferedReader(new FileReader(Shared.saveLocation + "Settings.txt"));
        while (line != null) {
            try {
                //System.out.println("COMES HERE");
                line = reader.readLine();
                System.out.println(line);

            } catch (IOException e2) {
                // TODO Auto-generated catch block
                //e2.printStackTrace();
                //JOptionPane.showMessageDialog(null, "Error Code: Bx001", "Error", 0);

            }
        }
    }

Enjoy :) we all make mistakes.

Also this example sucks you probably took it from there. http://www.cafeaulait.org/slides/sd2000east/javaio/56.html

Upvotes: 1

Maciej Stachowski
Maciej Stachowski

Reputation: 1728

Move the "new BufferedReader" line outside of the while loop.

if (f.exists()) {
        reader = new BufferedReader(new FileReader(Shared.saveLocation + "Settings.txt"));
        while (line != null) {
            try {
                //System.out.println("COMES HERE");

                line = reader.readLine();
                System.out.println(line);

            } catch (IOException e2) {
                // TODO Auto-generated catch block
                //e2.printStackTrace();
                //JOptionPane.showMessageDialog(null, "Error Code: Bx001", "Error", 0);

            }
        }
    }

Upvotes: 1

Grzegorz Żur
Grzegorz Żur

Reputation: 49161

Put reader before while loop. You are creating multiple readers and then you read only first line.

reader = new BufferedReader(new FileReader(Shared.saveLocation + "Settings.txt"));
while (line != null) {
    try {
        line = reader.readLine();
        System.out.println(line);

Upvotes: 0

David
David

Reputation: 2702

You're creating a new BufferedReader against the file inside the while loop, so the reader is basically reset every time the loop executes. Move the line that creates the reader outside the loop and give it a go.

Upvotes: 6

Related Questions