我想通过读取文本文件来创建一个 java 对象

I wanted to create a java Object by reading a text file

文本文件包含以下数据:

Rock
12
10
0

我想使用此数据并将其作为属性添加到对象。 对象名称是玩家,属性是:

所以当创建对象时 player.name 是 "Rock",player.maximum_health 是 12,player.current_health 是 10 和 player.no_of_wins 是 0.

您可以使用 Regex 命名组来获取它们或使用 space

拆分
public static void main(String... args) {
    String text = "Rock 12 10 0";
    Pattern pattern = Pattern.compile("([a-zA-Z0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)");
    Matcher matcher = pattern.matcher(text);
    while (matcher.find()) {
        String name = matcher.group(1);
        String maximum_health = matcher.group(2);
        String current_health = matcher.group(3);
        String no_of_wins = matcher.group(4);
        Player player = new Player(name, maximum_health, current_health, no_of_wins);
    }
}