在 Java 中使用 FileReader 和 BufferedReader 正确读取文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22741638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:37:16  来源:igfitidea点击:

Properly reading files with FileReader and BufferedReader in Java

javafilebufferedreader

提问by Rohit Tigga

I am trying to learn how to use read files using FileReader in Java however I get persistent errors. I am using Eclipse and I get a red error indicating that The constructor FileReader(File) is undefined and The constructor BufferedReader(FileReader) is undefined; however, I do not know where this error is originating from because I am using the right libraries and statements.

我正在尝试学习如何在 Java 中使用 FileReader 读取文件,但是我遇到了持久性错误。我正在使用 Eclipse,我收到一个红色错误,表明构造函数 FileReader(File) 未定义,构造函数 BufferedReader(FileReader) 未定义;但是,我不知道此错误的来源,因为我使用了正确的库和语句。

I get the following error:

我收到以下错误:

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)

My code is below:

我的代码如下:

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) {
            }
        }



    }

}

For extra context (Sorry about the size but I believe you can zoom in. You can see where to red errors are on the left of the line): enter image description here

对于额外的上下文(对不起,大小,但我相信你可以放大。你可以看到行左侧的红色错误在哪里): 在此处输入图片说明

回答by Jabir

Try following

尝试以下

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 {
            java.io.FileReader fr = new java.io.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) {
            }
        }



    }

}

Actually your class FileReaderis hiding java.io.FileReader. Above should work now

其实你的班级FileReader是隐藏的java.io.FileReader。以上应该现在工作

回答by chrylis -cautiouslyoptimistic-

The problem is that you named your own class FileReader, and it's conflicting with the java.io.FileReaderyou want to use. This is what the red line under the import is telling you: Importing won't work because you have a different class with the same name that's shadowing the import. Change the name of your class.

问题是你命名了你自己的 class FileReader,它与java.io.FileReader你想要使用的冲突。这就是导入下的红线告诉您的:导入将不起作用,因为您有一个同名的不同类隐藏了导入。更改您的班级名称。