使用BufferedReader和FileReader;文件正在读取但不输出其内容?

时间:2016-11-26 16:36:56

标签: java bufferedreader filereader

我编写了一个应该使用BufferedReaderFileReader类读取外部文件的程序。它可以识别文件并成功构建,但它不会打印出它应该执行的文本文件的内容。这是代码:

程序

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class Lab9 {

    public static void main(String[] args) {

        BufferedReader reader = null;
        String line;
        Scanner sc = new Scanner(System.in);

        System.out.println("Please enter a file name to read");

        try {
            reader = new BufferedReader(new FileReader("C:\\Users\\Lee\\Documents\\NetBeansProjects\\Lab9\\" + sc.next()));
        } catch (FileNotFoundException ex) {

            System.out.println(ex.getMessage() + "File was not found");        

            try { 
                while ((line = reader.readLine()) != null)
                {
                    System.out.println(line);
                }
            } catch (IOException ex2) {
                System.out.println(ex2.getMessage() + "File did not read correctly");
            } finally {
                System.exit(0);
            }
        }
    }
}

应打印出来的文件内容如下所示:

文件内容

By what initials was Franklin Roosevelt better known?:FDR
Which number president was Franklin Roosevelt?:32
Which state was Franklin Roosevelt born in?:New York
In which year did Roosevelt become Governor of New York?:1929
What was the name of Franklin Roosevelt's wife?:Eleanor
How many children did Franklin Roosevelt have?:6
From which university did Franklin Roosevelt graduate with an A.B in history?:Harvard
What was the first name of Franklin Roosevelt's 5th cousin, who was also President?:Theodore
Which disease is believed to be the causes of Franklin Roosevelt's paralysis?:Polio
At what age did Franklin Roosevelt die?:63

实际输出

Please enter a file name to read
Questions.txt
BUILD SUCCESSFUL (total time: 6 seconds)

非常感谢有关解决此问题的任何帮助,谢谢。

4 个答案:

答案 0 :(得分:0)

您已尝试在FileNotFoundException的异常分支中读取该文件。这意味着,您的程序只有在找不到文件时才会读取文件,这没有任何意义。它的工作原理如下:

public static void main(String[] args) {

    BufferedReader reader = null;
    String line;
    Scanner sc = new Scanner(System.in);

    System.out.print("Please enter a file name to read: ");

    try {

        reader = new BufferedReader(
                new FileReader("C:\\Users\\Lee\\Documents\\NetBeansProjects\\Lab9\\" + sc.next()));

        try {

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

        } catch (IOException ex) {
            System.out.println(
                    ex.getMessage() + "File did not read correctly");
        } finally {
            try {
                if (reader != null)
                    reader.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    } catch (FileNotFoundException ex) {
        System.out.println("File was not found.");
    }

}

作为旁注,您应该始终关闭您使用的资源,在这种情况下,您的文件由FileReader打开,如上面finally分支所示。此外,您无需致电System.exit(0)

答案 1 :(得分:0)

第一个catch语句没有结束括号,因此文件阅读器位于catch语句中。

答案 2 :(得分:0)

只有在抛出Table 1 |User_ID (primary)| Name | Address | Email | Username | Password | Table 2 | Product_ID (Primary Key) | User_ID (Foreign Key) | 时才尝试读取文件。您也应该在使用它们后关闭资源。那这个呢?

FileNotFoundException

答案 3 :(得分:0)

public static void main(String[] args) throws IOException {

        BufferedReader reader = null;
        String line;
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter a file name to read");
            reader = new BufferedReader(new FileReader(
                    "C:\\Users\\Lee\\Documents\\NetBeansProjects\\Lab9\\"
                            + sc.next()));
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
        }