I'm just trying to read text from an existing file.txt But this program shows 2 errors
for the FileReader(file)) it says : Expected 0 arguments but found 1
and for reader.readLine() it says : Cannot resolve method 'readLine' in 'BufferedReader'
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReader {
public static void main(String[] args) {
File file = new File("fileExample.txt");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
} catch (IOException e) {
e.printStackTrace();
}
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
}
Rename your class to something other than BufferedReader
, and import the right class from the JDK:
import java.io.BufferedReader;
Otherwise the compiler will look for a constructor of your own class.
Note about exception handling: given the code you have, if an IOException occurs when creating the BufferedReader, then the subsequent code will throw a NullPointerException. It may be better to just wrap the entire code in a try-with-resources block, or have the main method throws IOException
.