在java中读取并使用文件中的嵌套列表

在java中读取并使用文件中的嵌套列表,java,Java,我在一个文本文件中有这种格式的数据,[[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].][/code>,我想用每个内部列表中的数字来读取它。到目前为止,我能够用下面的代码阅读每个列表的内部列表,如何扩展它以获得数字?这只处理将字符串读入数组列表的情况,我还没有找到处理我的情况的方法 File f = new File("route.txt"); Scan

我在一个文本文件中有这种格式的数据,
[[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].][/code>,我想用每个内部列表中的数字来读取它。到目前为止,我能够用下面的代码阅读每个列表的内部列表,如何扩展它以获得数字?这只处理将字符串读入数组列表的情况,我还没有找到处理我的情况的方法

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);
    }
}

如前所述,如果它是一个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);
    }
}

将其解析为JSON数组。我推荐


将其解析为JSON数组。我推荐


尝试将其解析为JSON数组。使用:
double[][]matrix=new ObjectMapper().readValue(new FileInputStream(f),double[][]class)尝试将其解析为JSON数组。使用:
double[][]matrix=new ObjectMapper().readValue(new FileInputStream(f),double[][]class)我得到一个处理序列(数字)的错误;也未定义。我假设对于String[]sequences=content.split([^\\d,]+),参数应该有一个结束字符串,即String[]sequences=content.split([^\\d,]+”)?是的,ProcessNumber只是指示处理一个数字序列的位置。改进的回答我得到一个错误,processSequence(数字);未定义。我还假设,对于String[]sequences=content.split([^\\d,]+),参数应该有一个结束字符串,即String[]sequences=content.split([^\\d,]+”)?是的,ProcessNumber只是指示在何处处理一个数字序列。改进的回答即使从maven central repo直接下载JSON-P依赖项,并且在您提供的linkJsonReader中粘贴这些依赖项后,JSON-P依赖项似乎也无法在我的eclipse上工作。您是否可以使用org.json重新编写您的解决方案?
org.json
很难看。我建议使用现代依赖管理工具,如Maven、Gradle、,正如我前面所说,我从maven central repo将依赖项添加到pom.xml中,这些依赖项起作用:即使直接从maven central repo下载JSON-P依赖项并将它们粘贴到您提供的linkJsonReader中,它们似乎也无法在我的eclipse上起作用。您是否可以使用org.json重新编写您的解决方案?
org.json
很难看。我建议使用现代依赖项管理工具,如Maven、Gradle或Ivy。正如我前面所说,我从Maven central report将依赖项添加到pom.xml中,这些依赖项可以工作:
[0, 0, 0] 22
[0, 0, 1] 34
[0, 0, 2] 56
[0, 1, 0] 12
[0, 1, 1] 31
[0, 1, 2] 44
[0, 2, 0] 74
[0, 2, 1] 18
[0, 2, 2] 53
[1, 0, 0] 15
[1, 0, 1] 16
[1, 0, 2] 18
[1, 1, 0] 90
[1, 1, 1] 89
[1, 1, 2] 74
[1, 2, 0] 44
[1, 2, 1] 32
[1, 2, 2] 13
[2, 0, 0] 13
[2, 0, 1] 15
[2, 0, 2] 17
[2, 1, 0] 1
[2, 1, 1] 4
[2, 1, 2] 7
[2, 2, 0] 88
[2, 2, 1] 73
[2, 2, 2] 10