BufferedReader什么都读不到然后终止程序,这是为什么?

BufferedReader reads nothing and then terminates the program, why is this?

我正在为一项作业编写一个程序,在该程序中,该国每所高中都向我提供人口数据和儿童贫困率,而我应该做的只是读取某些数据列并将它们写入另一列新的文本文件,然后另一个程序打印出此信息。我尝试通过创建一个 while 循环来读取文件的每一行(13486 行),将每一行的某些索引分配给它们相应的数组和数组索引作为字符串,然后移动到下一个 while 循环,其中这些数据是印在新的专栏中。从那里,我想对每个状态的值进行平均并在第二个程序中打印它们。但是,我什至还没有。

import java.io.*;
import java.util.ArrayList;

public class Main
{

public static void main(String[] args)
        throws FileNotFoundException, IOException
{
    String[] states =
    { "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA",
            "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD",
            "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ",
            "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC",
            "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY" };
    File censusData = new File(args[0]);
    if (!censusData.exists())
    {
        System.err.print("Census Data not found.");
    }
    else
    {
        System.out.println("This file definitely exists.");
    }
    // Scanner cdr = new Scanner(censusData);
    // cdr.useDelimiter("\n");
    FileReader fr = new FileReader(censusData);
    BufferedReader br = new BufferedReader(fr);

    ArrayList<String> lines = new ArrayList<String>();
    int[] stateCode = new int[13486];
    int[] population = new int[13486];
    int[] childPopulation = new int[13486];
    String line = new String();
    int i = 0;
    while ((line = br.readLine()) != null)
    {
        stateCode[i] = Integer.parseInt(line.substring(0, 1));
        population[i] = Integer.parseInt(line.substring(82, 89));
        childPopulation[i] = Integer.parseInt(line.substring(91, 98));
        i++;

    }
    br.close();
    File cD = new File(args[1], "newCensusData.txt");
    PrintWriter pw = new PrintWriter(cD);
    i = 0;
    while (childPopulation.length < Integer.parseInt(args[2]))
    {
        pw.printf("%4s", stateCode[i], population[i], childPopulation[i]);
        i++;

    }
    pw.close();

}
}

args[0]是人口普查信息文件的完整路径,args[1]是输出目录,args[2]是输入文本文件中的条目数。

实际情况是,根据调试器的说法,第一个 while 循环运行了两次,然后程序终止,我不知道为什么。文件位置,输出位置,其他一切都很好。 newCensusData.txt 甚至生成,但其中没有任何信息。谁能告诉我为什么 BufferedReader 对象实际上没有读取文本文件的行?

编辑:我认为这与我选择关闭不同 reader/writer 对象的位置没有太大关系。即使我不关闭它们中的任何一个,结果也是一样的。

您正在关闭循环内的BufferedReader(和PrintWriter),并且根据docs:

Once the stream has been closed, further read(), ready(), mark(), reset(), or skip() invocations will throw an IOException.

将循环更改为:

while ((line = br.readLine()) != null)
{

    stateCode[i] = Integer.parseInt(line.substring(0, 1));
    population[i] = Integer.parseInt(line.substring(82, 89));
    childPopulation[i] = Integer.parseInt(line.substring(91, 98));
    i++;        
}
br.close();

您可以使用 try-with-resources 打开 BufferedReader。这样你就不必担心明确关闭你的 Reader 因为语句会处理它。

try (BufferedReader br = new BufferedReader(new FileReader(args[0]))) {
    String line;
    while ((line = br.readLine()) != null) {
        stateCode[i] = Integer.parseInt(line.substring(0, 1));
        population[i] = Integer.parseInt(line.substring(82, 89));
        childPopulation[i] = Integer.parseInt(line.substring(91, 98));
        i++;
    }
} catch (FileNotFoundException ex) {
    //Catch your exception
} catch (IOException ex) {
    //Catch your exception
}

来自文档:

The class BufferedReader, in Java SE 7 and later, implements the interface java.lang.AutoCloseable. Because the BufferedReader instance is declared in a try-with-resource statement, it will be closed regardless of whether the try statement completes normally or abruptly (as a result of the method BufferedReader.readLine throwing an IOException).