如何让BufferedReader仅读取CSV文件指定行?Java实现案例
问题描述
我正在为学校项目编写Java程序,读取格式为(date, state, fips code, # of cases, # of deaths)的新冠疫情CSV文本文件数据。该文件包含2021年1月1日至2023年3月23日的每日数据,总计6万余行冗余内容,但我仅需要最后55行的美国各地区累计病例数与死亡数。请问如何让BufferedReader仅读取这最后55行数据?
问题解决更新(2023年5月1日)
感谢各位解答,我已于昨日自行解决该问题。我通过在while循环中添加判断逻辑,确保仅读取以字符串"2023-03-23"开头的行。
实现代码
StateCovidStats类
import java.io.*; import java.util.*; public class StateCovidStats extends State { private int cases, deaths; // 默认构造方法 public StateCovidStats() { } // 带参构造方法 public StateCovidStats(String theName, int caseCount, int deathCount) { super(theName); cases=caseCount; deaths=deathCount; } public void displayStats() { System.out.println("----------------------------------------"); System.out.println(this.getName()+" Stats:"); System.out.println("Cases: "+this.getCases()); System.out.println("Deaths: "+this.getDeaths()); System.out.println("----------------------------------------"); } // Getter方法 public String getStateName() { return this.getName(); } public int getCases() { return cases; } public int getDeaths() { return deaths; } // Setter方法 public void setStateName(String newName) { this.setName(newName); } public void setCases(int caseCount) { cases=caseCount; } public void setDeaths(int deathCount) { deaths=deathCount; } @Override public String toString() { String s=""; s+=this.getStateName()+" Cases: "+this.getCases()+" Deaths: "+this.getDeaths(); return s; } public static String findMaxCases(ArrayList<StateCovidStats> list) { int greatest=Integer.MIN_VALUE; String nameOfMax=""; for(int i=0;i<list.size();i++) { if(list.get(i).getCases()>greatest) { greatest=list.get(i).getCases(); nameOfMax=list.get(i).getName(); } } return nameOfMax; } public static String findMaxDeaths(ArrayList<StateCovidStats> list) { int greatest=Integer.MIN_VALUE; String nameOfMax=""; for(int i=0;i<list.size();i++) { if(list.get(i).getDeaths()>greatest) { greatest=list.get(i).getDeaths(); nameOfMax=list.get(i).getName(); } } return nameOfMax; } public static String findMinCases(ArrayList<StateCovidStats> list) { int least=Integer.MAX_VALUE; String nameOfMin=""; for(int i=0;i<list.size();i++) { if(list.get(i).getCases()<least) { least=list.get(i).getCases(); nameOfMin=list.get(i).getName(); } } return nameOfMin; } public static String findMinDeaths(ArrayList<StateCovidStats> list) { int least=Integer.MAX_VALUE; String nameOfMin=""; for(int i=0;i<list.size();i++) { if(list.get(i).getDeaths()<least) { least=list.get(i).getDeaths(); nameOfMin=list.get(i).getName(); } } return nameOfMin; } public static void main(String[] args) { String line=""; long totDeath=0; long totCase=0; ArrayList<StateCovidStats> states=new ArrayList<StateCovidStats>(); try { BufferedReader br = new BufferedReader(new FileReader(".\\src\\us-states-Jan-2020-through-March-2023.txt")); while ((line = br.readLine()) != null ) { // 仅处理2023-03-23的数据行 if(line.startsWith("2023-03-23")) { String[] covid = line.split(","); // 以逗号为分隔符拆分数据 totDeath+=Integer.parseInt(covid[4]); totCase+=Integer.parseInt(covid[3]); StateCovidStats temp=new StateCovidStats(covid[1],Integer.parseInt(covid[3]),Integer.parseInt(covid[4])); boolean stateExists = false; for(int i=0; i<states.size();i++) { if(states.get(i).getName().equalsIgnoreCase(temp.getName())) { stateExists = true; break; } } if(!stateExists) { states.add(temp); } } } //end of while br.close(); } //end of try catch (IOException e) { e.printStackTrace(); } //end of catch // 输出统计结果 String maxCase=findMaxCases(states); String minCase=findMinCases(states); String maxDeath=findMaxDeaths(states); String minDeath=findMinDeaths(states); int maxCaseNum=0; int minCaseNum=0; int maxDeathNum=0; int minDeathNum=0; for(int i=0; i<states.size();i++) { if(states.get(i).getName().equals("New Jersey")) { states.get(i).displayStats(); } if(states.get(i).getName().equals(maxCase)) { maxCaseNum=states.get(i).getCases(); } if(states.get(i).getName().equals(minCase)) { minCaseNum=states.get(i).getCases(); } if(states.get(i).getName().equals(maxDeath)) { maxDeathNum=states.get(i).getDeaths(); } if(states.get(i).getName().equals(minDeath)) { minDeathNum=states.get(i).getDeaths(); } } System.out.println("State With Most Cases: "+maxCase+", "+maxCaseNum+" Cases"); System.out.println("State With Least Cases: "+minCase+", "+minCaseNum+" Cases"); System.out.println("State With Most Deaths: "+maxDeath+", "+maxDeathNum+" Deaths"); System.out.println("State With Least Deaths: "+minDeath+", "+minDeathNum+" Deaths"); System.out.println("Total US Cases: "+totCase); System.out.println("Total US Deaths: "+totDeath); System.out.println("------------------------------------"); System.out.println("AVG State Cases: "+totCase/55); System.out.println("AVG State Deaths: "+totDeath/55); for(int i=0; i<states.size();i++) { if(states.get(i).getName().equals("New Jersey")) { states.get(i).displayStats(); } } } //end of main } //end of class
State类
public class State { private String stateName,timeZone; private int population; private double density; // 默认构造方法 public State() { } // 带参构造方法 public State(String theName) { stateName=theName; } // 名称的Getter和Setter方法 public String getName() { return stateName; } public void setName(String theName) { stateName=theName; } }
内容的提问来源于stack exchange,提问作者Dzubek
相关产品推荐
相关产品推荐

