Java 使用streams读取文本文件并保存到BigInteger数组
读取文本文件、按Java 使用streams读取文本文件并保存到BigInteger数组,java,file,java-stream,biginteger,Java,File,Java Stream,Biginteger,读取文本文件、按,拆分数字并将所有数字保存到biginger数组中的正确方法是什么 BigInteger[] a = new BigInteger[1000]; try (Stream<String> stream = Files.lines(Paths.get(filePath))) { } catch (IOException e) { e.printStackTrace(); } 您可以使用,作为分隔符拆分流中的每个字符串,然后将它们转换为biginger数组 Bi
,
拆分数字并将所有数字保存到biginger
数组中的正确方法是什么
BigInteger[] a = new BigInteger[1000];
try (Stream<String> stream = Files.lines(Paths.get(filePath))) {
} catch (IOException e) {
e.printStackTrace();
}
您可以使用
,
作为分隔符拆分流中的每个字符串,然后将它们转换为biginger
数组
BigInteger[] array = stream.map(str -> str.split(","))
.flatMap(Arrays::stream)
.map(BigInteger::new)
.toArray(BigInteger[]::new);
或者正如@Lino建议的方法所说,您也可以使用Pattern
object来拆分字符串
Pattern pattern = Pattern.compile(",");
BigInteger[] array = stream.flatMap(pattern::splitAsStream)
.map(BigInteger::new)
.toArray(BigInteger[]::new);
输入文件是什么样子的?它有隔板吗?还是每行代表一个值?@springe就像我在原始帖子中说的,我想用
,
分隔符来分割它们。而不是使用stream.map(str->str.split(“,”).flatMap(Arrays::stream)
你可以使用预编译的java.util.regex.Pattern
,然后编写stream.flatMap(yourPattern::splitAsStream)
Pattern pattern = Pattern.compile(",");
BigInteger[] array = stream.flatMap(pattern::splitAsStream)
.map(BigInteger::new)
.toArray(BigInteger[]::new);