使用流来读取文本文件并保存到BigInteger数组中

问题描述 投票:0回答:1

读取文本文件,用,分割数字并将它们全部保存到BigInteger数组的正确方法是什么?

BigInteger[] a = new BigInteger[1000];
try (Stream<String> stream = Files.lines(Paths.get(filePath))) {

} catch (IOException e) {
    e.printStackTrace();
}

可以直接完成,还是应该先将整个文件另存为大String,然后用流拆分它?

String content = new String(Files.readAllBytes(Paths.get(filePath)));
java file stream biginteger
1个回答
2
投票

喜欢这个

BigInteger[] array = stream.map(str -> str.split(",")).flatMap(Arrays::stream).map(BigInteger::new)
            .toArray(BigInteger[]::new);
© www.soinside.com 2019 - 2024. All rights reserved.