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

Using streams to read a text file and save to the BigInteger array

读取文本文件、将数字除以 , 并将它们全部保存到 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)));

可以将stream中的每一个字符串以,作为分隔符进行分割,然后转换成BigInteger数组

BigInteger[] array = stream.map(str -> str.split(","))
                           .flatMap(Arrays::stream)
                           .map(BigInteger::new)
                           .toArray(BigInteger[]::new);

或者按照@Lino 建议的方法说 Reinstate Monica 你也可以使用 Pattern 对象来拆分字符串

Pattern pattern = Pattern.compile(",");

BigInteger[] array = stream.flatMap(pattern::splitAsStream)
                           .map(BigInteger::new)
                           .toArray(BigInteger[]::new);