合并文件中的行以对应给定的 header
Merging lines in a file to correspond with a given header
我有一个文件,其中包含一系列 header,后跟一行或多行对 header 的描述。类似于:
>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
我正在尝试合并文件的元素,以便每个描述都放在一行中并与其给定的 header 相对应。喜欢:
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();
}
这是实现此目的的一种方法。我使用的不是数组,而是 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);
}
}
输出:
Animals = [[Dog, this is a dog which barks], [Cat, Cats often meow],
[Bat, Bat will fly and comes out at night eating small insects]]
我有一个文件,其中包含一系列 header,后跟一行或多行对 header 的描述。类似于:
>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
我正在尝试合并文件的元素,以便每个描述都放在一行中并与其给定的 header 相对应。喜欢:
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();
}
这是实现此目的的一种方法。我使用的不是数组,而是 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);
}
}
输出:
Animals = [[Dog, this is a dog which barks], [Cat, Cats often meow], [Bat, Bat will fly and comes out at night eating small insects]]