![](/img/trans.png)
[英]Java split string on empty delimiter returns empty string at the beginning?
[英]Split a string in Java and insert it into an empty string
我有一个包含以下数据的 CSV 文件:
20210903|0000000001|0081|A|T60|BSN|002|STATE UNITED
我已使用以下代码将此文件导入到我的 Java 应用程序中:
public List<EquivalenceGroupsTO> read() throws FileNotFoundException, IOException {
try (BufferedReader br = new BufferedReader(new FileReader("/home/myself/Desk/blaBla/T60.csv"))) {
List<String> file = new ArrayList<String>();
StringBuilder sb = new StringBuilder();
String line = br.readLine();
Integer count = 0;
HashSet<String> hset = new HashSet<String>();
while (line != null) {
//System.out.println("data <" + count + "> :" + line);
count++;
file.add(line);
file.add("\n");
line = br.readLine();
}
EquivalenceGroupsTO equivalenceGroupsTO = new EquivalenceGroupsTO();
List<EquivalenceGroupsTO> equivalenceGroupsTOs = new ArrayList<>();
for (String row : file) {
equivalenceGroupsTO = new EquivalenceGroupsTO();
String[] str = row.split("|");
equivalenceGroupsTO.setEquivalenceGroupsCode(str[5]);
equivalenceGroupsTO.setDescription(str[7]);
equivalenceGroupsTO.setLastUpdateDate(new Date());
equivalenceGroupsTOs.add(equivalenceGroupsTO);
System.out.println("Tutto ok!");
}
return equivalenceGroupsTOs;
}
}
我需要在equivalenceGroupsTO.setEquivalenceGroupsCode和equivalenceGroupsTO.setDecription (它们是字符串)中分别设置第五个和第七个“|”之后的字符串 ,然后是“ BSN ”和“ STATE UNITED ”。
但是如果我启动这个脚本,它会给我这个错误:
java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 1
at it.utils.my2.read(OpenTXTCodifa.java:46)
我究竟做错了什么?
评论中提到了主要问题:当拆分时|
字符,它必须转义为\\\\|
因为管道字符在常规表达式中是用户作为 OR 运算符。
下一个问题是将仅包含\\n
的行添加到file
。 当这条线被拆str[5]
, str[5]
将失败并显示ArrayIndexOutOfBoundsException
。
其他小问题是未使用的变量count
和hset
。
但是,重构现有代码以使用 NIO 和 Stream API 获取行流并将每一行转换为对应的EquivalenceGroupsTO
列表可能会更好:
public List<EquivalenceGroupsTO> read(String filename) throws IOException {
return Files.lines(Paths.get(filename)) // Stream<String>
.map(s -> s.split("\\|")) // Stream<String[]>
// make sure all data are available
.filter(arr -> arr.length > 7) // Stream<String[]>
.map(arr -> {
EquivalenceGroupsTO egTo = new EquivalenceGroupsTO();
egTo.setEquivalenceGroupsCode(str[5]);
egTo.setDescription(str[7]);
egTo.setLastUpdateDate(new Date());
return egTo;
}) // Stream<EquivalenceGroupsTO>
.collect(Collectors.toList())
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.