合并文件中的行以与给定标头对应

时间:2016-06-04 22:53:00

标签: java arrays file bufferedreader

我有一个文件,其中包含一系列标题,后跟一行或多行标题的说明。类似的东西:

>Animal|Dog
this is a dog
which barks
>Animal|Cat
Cats often meow
>Animal|Bat
Bat will fly
and comes out at night
eating small insects

我试图合并文件的元素,以便将每个描述放入一行并对应于其给定的标题。像:

Animals = [Dog,this is a dog which barks],[Cat,Cats often meow],[Bat,Bat will fly and come out at night eating small insects]

我不确定如何做到这一点。

public static void main(String[] args) {

    try (BufferedReader br = new BufferedReader(new FileReader("speciesFile.txt"))) {
        String line;
        while ((line = br.readLine()) != null) {
            if (line.contains(">")) {

                String[] array = line.split("|");
                if(array.length == 2){
                    String name = array[1];
                    // add to array
                }
            }else{
                // line is description
                // check if line below is description and merge
                // add to array
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

1 个答案:

答案 0 :(得分:1)

这是实现这一目标的一种方法。我正在使用ArrayList而不是数组,我创建了一个名为Animal的简单模型对象来包含每个动物记录。

有关详细信息,请参阅代码中的注释。

public class MergeLines
{
    // Animal model object:
    // Note: This can placed in another file called Animal.java and
    // made into a public, non-static class, but I created it in this
    // manner for purpose of having a and self-containted example.
    private static class Animal
    {
        private String name;
        private String description;

        public Animal(String name)
        {
            this.name = name;
        }

        public void setDescription(String description)
        {
            this.description = description;
        }

        @Override
        public String toString ()
        {
            return "[" + name + "," + description + "]";
        }
    }

    public static void main (String[] args)
    {
        // this list will hold our animals
        List<Animal> animals = new ArrayList<Animal>();

        try (BufferedReader br = new BufferedReader(new FileReader("speciesFile.txt")))
        {
            String line;
            String description = "";
            Animal animal = null;
            while ((line = br.readLine()) != null)
            {
                if (line.contains(">"))
                {
                    // check if we had a previous animal
                    if (description.length() > 0 && animal != null)
                    {
                        // set the description and add to the list
                        animal.setDescription(description);
                        animals.add(animal);

                        // reset for the next animal
                        description = "";
                        animal = null;
                    }

                    // Note: you had split("|") but that is incorrect 
                    // since the '|' character means OR in regex.
                    String[] array = line.split("\\|");
                    if (array.length == 2)
                    {
                        String name = array[1];

                        // create the animal
                        animal = new Animal(name);
                    }
                }
                else
                {
                    description = description + " " + line;
                }
            }

            // add the last animal to the list
            if (animal != null)
            {
                animal.setDescription(description);
                animals.add(animal);
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        // finally, print them out.
        System.out.println("Animals = " + animals);
    }
}

<强>输出

  

动物= [[狗,这是一只吠叫的狗],[猫,猫经常喵喵],   [蝙蝠,蝙蝠会飞,晚上出来吃小昆虫]]