Java - unable to read from file with this code

37 Views Asked by At
public class RandomFuncs {

public static void main(String[] args) {
    readingFile();
}
public static void readingFile() {
    String file = "coins.txt";
    try {
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        String line = bufferedReader.readLine();

        //System.out.println(line);

        while (line != null){ System.out.println(line);}
    }
    catch (IOException ex){
        System.out.println("Unable to read to file " + file + ".Due to " + ex);
    }
}

}

variable line always comes back as null and I'm not sure why . There is lines of text in the file but it's not being read.

1

There are 1 best solutions below

0
Ritielko On

Try putting the absolute file path into 'file'-variable.

Also

String line = bufferedReader.readLine();

while (line != null){ System.out.println(line);}

doesn't work, it prints 'line' until that particular string turns into null, which is never. Script just keeps printing the first line in the file forever.