如何跳过文本文件的前3行?

Dil*_*mal 0 java

当我使用Java读取文本文件时,如何跳过文本文件的前三行?

目前的节目,

public class Reader {
    public static void main(String[] args) {
        BufferedReader reader;
        try {
            reader = new BufferedReader(new InputStreamReader(
            new FileInputStream("sample.txt")));
            Map<String, Integer> result = new LinkedHashMap<String, Integer>();
            Map<String, Integer> result2 = new LinkedHashMap<String, Integer>();
            while (reader.ready()) {
                String line = reader.readLine();
                //split a line with spaces
                String[] values = line.split("\s+");
                //set a key date\tanimal
                String key = values[0] + "\t" + values[1];
                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 + " " + sum + "\t" + count);
            }
            reader.close();
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Ara*_*ram 6

为什么你不能这样做?

//Skip 3 lines.
for(int i=1;i<=3;i++)
{
    reader.readLine();
}
Run Code Online (Sandbox Code Playgroud)

您可以通过引入这两个类来简化代码并使其更具可读性.然后你只能维护1张地图,

Map<Animal, Summary> result = new HashMap<Animal, Summary>();

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;
    }
}
Run Code Online (Sandbox Code Playgroud)