我正在为一个任务编写程序,其中该国的每所高中都向我提供了人口数据和儿童贫困率,而我要做的就是仅读取某些数据列并将其写入另一新文本中文件,然后另一个程序打印出此信息。我尝试通过创建一个while循环来做到这一点,该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
对象实际上没有读取文本文件的行吗?
编辑:我认为在我选择关闭其他读取器/写入器对象的位置上,这没有多大作用。即使我没有关闭它们中的任何一个,结果也是一样。
答案 0 :(得分:2)
您正在关闭循环中的 @Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("Authorization", "Bearer AccesToken");
return headers;
(和BufferedReader
),并根据docs:
关闭流后,进一步的read(),ready(),mark(),reset()或skip()调用将引发IOException。
将循环更改为:
PrintWriter
答案 1 :(得分:1)
您可以使用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
}
从文档中:
Java SE 7和更高版本中的BufferedReader类实现接口java.lang.AutoCloseable。因为BufferedReader实例是在try-with-resource语句中声明的,所以无论try语句是正常完成还是突然完成(由于BufferedReader.readLine方法抛出IOException),它都会关闭。