首页 > 解决方案 > 如何从具有动态条目的文件中拆分

问题描述

我正在尝试拆分许多从文件中读取的字符串,
这是文件中的一些内容:

Jack, 50, Aldwynne, Field1, Field2, Field3, Field4, Field5
Goe, 23, Clearden, Field1, Field2, Field3, Field4, Field5
Mike, 33, Marblesilver, Field1, Field2
Lupis, 38, Aldfair, Field1, Field2, Field3
Frey, 21, Verttown, Field1, Field2, Field3, Field4, Field5
Zulian, 45, Fogedge, Field1, Field2, Field3, Field4, Field5

我的代码:

BufferedReader br = new BufferedReader(new FileReader(FILENAME))
String currLine;
while ((currLine = br.readLine()) != null) {
    String []arr = currLine.trim().split("\\s*,[,\\s]*");

String first=arr[0];
String second=arr[1];
String third=arr[2];
String fourth=arr[3];
String fifth=arr[4];
String sixth=arr[5];
String seventh=arr[6];
String eighth=arr[7];

// Next Process
}

然后我将它们中的每一个存储到变量中。前两行很好,但问题是第三行会产生错误ArrayIndexOutOfBounds,因为它没有第四个索引。因此,程序停在第 3 行

那么如何将空字符串("")设置为长度低于 8 的行中的缺失索引。我想要的如下所示(例如:第 3 行)

String first="Mike";
String second="33";
String third="Marblesilver";
String fourth="Field1";
String fifth="Field2";
String sixth="";
String seventh="";
String eighth="";

等等。我怎么做 ?

标签: javaarraysstringfilesplit

解决方案


我想用空字符串完成其他字段,如果您使用的是 Java 8,您可以像这样读取文件:

String fileName = "file.txt";
String regex = "\\s*,[,\\s]*";
int maxLength = 8;//This should be know from the beginning 
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
    stream.map(line -> {
        List<String> list = new ArrayList<>(Arrays.asList(line.split(regex)));
        List<String> emptyFields = IntStream.range(0, maxLength - list.size())
                .mapToObj(s -> "")
                .collect(Collectors.toList());
        list.addAll(list.size(), emptyFields);
        return list;
    }).forEach(System.out::println);
} catch (IOException e) {
    e.printStackTrace();
}

在你的情况下它会告诉你:

[Jack, 50, Aldwynne, Field1, Field2, Field3, Field4, Field5]
[Goe, 23, Clearden, Field1, Field2, Field3, Field4, Field5]
[Mike, 33, Marblesilver, Field1, Field2, , , ]           //note the empty fields in the end
[Lupis, 38, Aldfair, Field1, Field2, Field3, , ]
[Frey, 21, Verttown, Field1, Field2, Field3, Field4, Field5]
[Zulian, 45, Fogedge, Field1, Field2, Field3, Field4, Field5]

除此之外,我将创建一个 MyObject 类,它保存此信息而不是数组,因此您的代码应如下所示:

public class MyObject {

    private String first;
    private String second;
    private String third;
    private String fourth;
    private String fifth;
    private String sixth;
    private String seventh;
    private String eighth;

    //...Constructor, Getters, Setter
}

...
List<MyObject> result = stream.map(line -> {
    List<String> list = new ArrayList<>(Arrays.asList(line.split(regex)));
    List<String> emptyFields = IntStream.range(0, maxLength - list.size())
            .mapToObj(s -> "")
            .collect(Collectors.toList());
    list.addAll(list.size(), emptyFields);
    return list;
}).map(line -> new MyObject(line.get(0),
        line.get(1), line.get(2), line.get(3), line.get(4),
        line.get(4), line.get(5), line.get(6))
).collect(Collectors.toList());
...

推荐阅读