我在[ [[22,34,56],[12,31,44],[74,18,53]], [[15,16,18],[90,89,74],[44,32,13]], [[13,15,17],[1,4,7],[88,73,10]]...]
文本文件中有此格式的数据,我想使用每个内部列表中的数字来读取它。到目前为止,我可以使用下面的代码读取列表的每个内部列表,如何扩展它以获取数字?这个问题只处理将字符串读入数组列表的情况,而我还没有找到处理我的情况的另一种情况。
File f = new File("route.txt");
Scanner s = new Scanner(new FileInputStream(f));
s.useDelimiter("]],");
while (s.hasNext()) {
String r = s.next();
System.out.println(r);
}
如前所述,如果它是JSON数组,则可以执行此操作。但是随后需要深入研究结果数据结构以进行处理。自己动手的解决方案:
Path path = Paths.get("route.txt");
byte[] bytes = Files.readAllBytes(path);
String content = new String(bytes, StandardCharsets.ISO_8859_1);
content = content.replace(" ", ""); // So spaces at odd places do not need to be handled.
String[] sequences = content.split("[^\\d,]+"); // Delimit by not: digit or comma.
for (String sequence : sequences) {
if (!sequence.isEmpty()) {
String[] words = sequence.split(",");
int[] numbers = Stream.of(words).mapToInt(Integer::parseInt).toArray();
.. process the sequence(numbers);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句