Java - 从文件中读取结构松散的数据

Java - Read loosely structured data from file

我目前正在尝试了解 Java 代码的行为方式以及如何处理输入和输出文件。就我了解如何逐行读取文件内容并将它们放入数组而言,我很难理解如何从文件中读取每隔 n 行出现的某些值,然后将它们放入数组. 例如,我有一个如下所示的输入测试文件:

2
Australia
John
42
Blue
USA
Jeremmy
15
Black

第一行是一个数组的大小。以下几行是我想从文件中读取并放入所述数组中的内容(在这个例子中,我将其设为:国家、姓名、年龄、眼睛颜色)。换句话说,我想读取每四行出现的一些对象属性,以便稍后在我选择时将它们打印出来,例如其中一个人。 现在我被困在这里,不知道如何前进,因为大多数人都没有尝试对这样的文件进行操作。

   private static String[] readPeople(File file) {
    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
        String sizeText = br.readLine();
        int size = Integer.parseInt(sizeText);
        String[] peopleSet= new String[size];

        for (int i = 1; i < size; i++) {
            if (i % 2 != 0) {
                String peopleInfo= br.readLine();
                peopleSet[i] = peopleInfo;
            }
        }
        return peopleSet;
    } catch (IOException ex) {
        System.err.println(ex);
        return new String[0];
    }
}

我给你这个代码。

    File Read_File = new File("file_path"); 

    BufferedReader br = null;

    try {
        br = new BufferedReader(new FileReader(Read_File));
        String line;
        int line_number = 0;

        int size = Integer.parseInt(br.readLine());



        for (int try_ = 0; try_ < size; try_++) {
            String read = br.readLine();
            for(int try_four = 0; try_four<4; try_four++) {
                //save input operation
            }
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null)
            try {
                br.close();
            } catch (IOException e) {
            }
    } 

但最好使用列表。

用冒号分隔行可能更容易

您的条目是否在文件中的值之间用冒号分隔:

Australia:John:42:Blue
USA:Jeremmy:15:Black

然后在你的文件解析器中:

private static List<Person> readPeople(File file) {
    List<Person> people = new ArrayList<>();
    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
      String line = "";
      while((line = br.readLine()) != null) {
        String[] args = line.split(":");
        String country = args[0];
        String name = args[1];
        int age = Integer.parseInt(args[2]);
        String eyeColor = args[3];

        Person p = new Person(name, country, age, eyeColor);
        people.add(p);
      }
    } catch (IOException ex) {
      System.err.println(ex);
    } 
    return people;
}

最后定义人物class

class Person {
   String name;
   String country;
   int age;
   String eyeColor;

   public Person(String name, String country, int age, String eyeColor) {
      this.name = name;
      this.country = country;
      this.age = age;
      this.eyeColor = eyeColor;
   }

   @Override
   public String toString() {
     return String.format("%s:%s:%d:%s", country, name, age, eyeColor);
   }
}

根据需要添加错误检查和getters/setters

这将 return 文件中定义的人员列表,您可以通过调用 return 列表对象上的 .size() 检查列表的大小。

更新

添加了 toString() 覆盖以创建冒号分隔的条目,以便将其写入文件或控制台

希望对您有所帮助

问题是您的代码仅在条件为真时才从文件中读取。不管你循环多少次,文件内容只会在你用br.readLine().

实际阅读时前进
for (int i = 1; i < size; i++) {
    if (i % 2 != 0) {
        String peopleInfo= br.readLine();
        peopleSet[i] = peopleInfo;
    }
}

也不清楚您想阅读哪几行。此代码将循环 size 次 (2) 但每隔一次只读取一行,因此它只会读取 "Australia."

如果你想读出人名("John" 和 "Jeremmy"),这是每组四行中的第三行,你可以这样做:

for (int i = 1; i < size; i++) {
    for (int j = 0; j < 4; j++) {
        String line = br.readLine();
        if (j % 4 != 2) {
            people[i] = line;
        }
    }
}

这会读取所有八行(从 "Australia" 到 "Black")。对于每个人,j 的值将从 0 变为 3;因此,第三行是 j % 4 == 2.

它会将名称存储到长度为 2 的数组 people 中。我将其从 peopleSet 重命名,因为它不是 Java Set。原名误导。