Roh*_*gga 3 java file bufferedreader
我试图学习如何使用Java中的FileReader来读取文件但是我遇到了持久性错误.我正在使用Eclipse,我得到一个红色错误,表明构造函数FileReader(File)未定义,构造函数BufferedReader(FileReader)未定义; 但是,我不知道这个错误源自何处,因为我正在使用正确的库和语句.
我收到以下错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The constructor FileReader(File) is undefined
The constructor BufferedReader(FileReader) is undefined
at FileReader.main(FileReader.java:17)
Run Code Online (Sandbox Code Playgroud)
我的代码如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileReader {
public static void main(String[] args) {
File file = new File("example.txt");
BufferedReader br = null;
try {
FileReader fr = new FileReader(file);
br = new BufferedReader(fr);
String line;
while( (line = br.readLine()) != null ) {
System.out.println(line);
}
} catch (FileNotFoundException e) {
System.out.println("File not found: " + file.toString());
} catch (IOException e) {
System.out.println("Unable to read file: " + file.toString());
}
finally {
try {
br.close();
} catch (IOException e) {
System.out.println("Unable to close file: " + file.toString());
}
catch(NullPointerException ex) {
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有关额外的背景信息(对不起,我相信你可以放大.你可以看到线路左侧红色错误的位置):

问题是你命名了自己的类FileReader,它与java.io.FileReader你想要使用的类冲突.这就是导入下面的红线告诉您:导入将无效,因为您有一个不同的类,其名称与导入的阴影相同.更改班级的名称.