使用java和maped filterd数据读取行

public class Reader { public static void main(String[] args) throws IOException, ParseException { BufferedReader reader; String animalName="cat"; String animal = null; try { reader = new BufferedReader(new InputStreamReader( new FileInputStream("C:/dila.txt"))); Map result = new LinkedHashMap(); Map result2 = new LinkedHashMap(); while (reader.ready()) { String line = reader.readLine(); /split a line with spaces/ String[] values = line.split(","); String key = null; if(values[1].compareTo(animalName)==0){ key = values[0]; animal=""+values[1].compareTo(animalName); int sum = 0; int count = 0; /get a last counter and sum/ if (result.containsKey(key)) { sum = result.get(key); count = result2.get(key); } else{ } /increment sum a count and save in the map with key/ result.put(key, sum + Integer.parseInt(values[2])); result2.put(key, count + 1); } } /interate and print new output/ for (String key : result.keySet()) { Integer sum = result.get(key); Integer count = result2.get(key); System.out.println(key +" "+animalName+ " " + sum + "\t" + count); } reader.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 
  • 我有下面的文本文件
    11/2/2010年,猫,6
    11/2/2010年,猫,3
    11/2/2010年,狗,4
    11/2/2010年,猫,11
    11/3/2010年,猫,1
    11/3/2010年,狗,3
    11/3/2010年,猫,8
    11/3/2010年,猫,80
  • 上面的代码目前正在打印此摘要数据
    11/2/2010 cat 20 3
    11/3/2010 cat 104 4
    11/4/2010 cat 26 2
  • 我需要帮助才能打印摘要,如下所示
    11/01/2010
    11/02/2010 cat 20 3
    11/03/2010 cat 104 4
    11/04/2010 cat 26 2
    11/05/2010
    11/06/2010
    11/07/2010
    11/08/2010
    11/09/2010
    11/10/2010
    11/11/2010
    11/12/2010
    11/13/2010
    2010年11月14日
    11/15/2010
    11/16/2010
    11/17/2010
    11/18/2010
    11/19/2010
    2010年11月20日
    2010年11月21日
    11/22/2010
    11/23/2010
    11/24/2010
    11/25/2010
    11/26/2010
    2010年11月27日
    11/28/2010
    11/29/2010
    11/30/2010

我从“,”中分离出大量数据。 所以我想读线和拆分。 我已经做到了。 但我的要求是上面显示的结果。

以下是执行此操作的代码。 我正在寻求google-guava库的帮助,因为它可以帮助我编写更少的代码;-)。 如果你只想在普通的java中然后你也可以修改代码,如果逻辑需要一些调整然后看看processLine(…)方法,那就是改变将去的地方

好吧,我看到的唯一缺失的代码是按排序顺序打印不属于输入文件的日期的空数据。 这很简单,留给你。 以下是提示:将日期递增1并循环到月末

我已经运行了您的示例文件并打印了以下摘要

 11/3/2010 cat 89 3 11/3/2010 dog 3 1 11/2/2010 dog 4 1 11/2/2010 cat 20 3 import java.io.File; import java.io.IOException; import java.util.Iterator; import java.util.Map; import com.google.common.base.CharMatcher; import com.google.common.base.Charsets; import com.google.common.base.Joiner; import com.google.common.base.Splitter; import com.google.common.collect.Maps; import com.google.common.io.Files; import com.google.common.io.LineProcessor; public class AnimalSummaryBuilder { private static final Splitter SPLITTER = Splitter.on(CharMatcher.anyOf(",")); private static final Joiner JOINER = Joiner.on("\t"); @SuppressWarnings("unchecked") public static void main(final String[] args) throws Exception { @SuppressWarnings("rawtypes") Map result = Files.readLines(new File("c:/1.txt"), Charsets.ISO_8859_1, new LineProcessor() { private final Map result = Maps.newHashMap(); public Object getResult() { return result; } public boolean processLine(final String line) throws IOException { Iterator columns = SPLITTER.split(line).iterator(); String date = columns.next(); String name = columns.next(); int value = Integer.valueOf(columns.next()).intValue(); Animal currentRow = new Animal(date, name); if (result.containsKey(currentRow)) { Summary summary = result.get(currentRow); summary.increaseCount(); summary.addToTotal(value); } else { Summary initialSummary = new Summary(); initialSummary.setCount(1); initialSummary.setTotal(value); result.put(currentRow, initialSummary); } return true; } }); for (Map.Entry entry : result.entrySet()) { Animal animal = entry.getKey(); Summary summary = entry.getValue(); System.out.println(JOINER.join(animal.date, animal.name, summary.total, summary.count)); } } final static class Animal { String date; String name; public Animal(final String date, final String n) { this.date = date; this.name = n; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((date == null) ? 0 : date.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof Animal)) { return false; } Animal other = (Animal) obj; if (date == null) { if (other.date != null) { return false; } } else if (!date.equals(other.date)) { return false; } if (name == null) { if (other.name != null) { return false; } } else if (!name.equals(other.name)) { return false; } return true; } } final static class Summary { private int total; private int count; void setTotal(int value) { total = value; } void setCount(int i) { count = i; } void increaseCount() { count++; } void addToTotal(int valueToAdd) { total += valueToAdd; } } } 

您可以使用另一个以日期为关键字的地图,以及您获得的结果作为值。 然后你只需遍历月中的所有日子,如果地图包含当前日期键,则打印相应的值,否则只打印日期。

这是肮脏的解决方案。 假设“结果”地图仅包含1个月。

 public class Reader { public static void main(final String[] args) throws ParseException { BufferedReader reader = null; String animalName = "cat"; // String animal = null; try { reader = new BufferedReader(new InputStreamReader(new FileInputStream("C:/1.txt"))); Map result = new LinkedHashMap(); Map result2 = new LinkedHashMap(); while (reader.ready()) { String line = reader.readLine(); // split a line with spaces String[] values = line.split(","); String key = null; if (values[1].compareTo(animalName) == 0) { key = values[0]; // animal=""+ ""+values[1].compareTo(animalName); int sum = 0; int count = 0; // get a last counter and sum if (result.containsKey(key)) { sum = result.get(key); count = result2.get(key); } else { } // increment sum a count and save in the map with key result.put(key, sum + Integer.parseInt(values[2])); result2.put(key, count + 1); } } String date = result.keySet().iterator().next(); DateFormat df = new SimpleDateFormat("mm/dd/yyyy"); Calendar calendar = Calendar.getInstance(); calendar.setTime(df.parse(date)); int monthStart = calendar.getActualMinimum(Calendar.DAY_OF_MONTH); int monthEnd = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); calendar.set(Calendar.DAY_OF_MONTH, 0); // interate and print new output for (int i = monthStart; i < monthEnd; i++) { calendar.add(Calendar.DAY_OF_MONTH, 1); String key = df.format(calendar.getTime()); if (result.containsKey(key)) { Integer sum = result.get(key); Integer count = result2.get(key); System.out.println(key + " " + animalName + " " + sum + "\t" + count); } System.out.println(key); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { reader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }