繁体   English   中英

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

[英]Properly reading files with FileReader and BufferedReader in Java

我试图学习如何使用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)

我的代码如下:

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



    }

}

有关额外的背景信息(对不起,我相信你可以放大。你可以看到线路左侧红色错误的位置): 在此输入图像描述

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

试试以下

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



    }

}

实际上你的类FileReader正在隐藏java.io.FileReader 以上应该现在工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM