如何快速将大型txt文件加载到BigInteger中? [英] How can I quickly load a large txt file into BigInteger?
问题描述
我正在导入一个大文本文件, 1700万位长我正在使用此代码:
I'm importing a large text file, 17 million digits long and I'm using this code:
BufferedReader reader = new BufferedReader(new FileReader("test2.txt"));
String line = reader.readLine();
System.out.println("Done");
BigInteger num = new BigInteger(line);
System.out.println("Done Again");
它立即加载文件并打印出'完成'
但要将 String
转换为 BigInteger
,有什么我可以做的吗加快速度并快速加载数字?
It loads the file pretty much instantly and prints out 'Done'
but it takes a long time (about an hour) for the String
to be converted into a BigInteger
, is there anything I can do to speed this up and quickly load the number?
推荐答案
作为优化,因为 BigInteger
Serializable
,您可以将其保存到二进制文件一次,并大大加快您的加载速度。
As an optimization, since BigInteger
is Serializable
, you could save it to a binary file once and speed up your loading considerably.
加载序列化对象应该是紧固 r,而不是每次解析一个巨大的字符串。
Loading a serialized object should be way faster than parsing a huge string everytime.
使用 ObjectOutputStream
您的大整数和 ObjectInputStream
将其重读。
Use ObjectOutputStream
to save your big integer and ObjectInputStream
to read it back in.
这篇关于如何快速将大型txt文件加载到BigInteger中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!