Java – how to quickly load large txt files into BigInteger?
•
Java
I'm importing a large 17 million bit text file. 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");
It loads the file almost immediately and prints out "done", but it takes a long time (about an hour) to convert the string to BigInteger. What can I do to speed up and load the numbers quickly?
Solution
As an optimization, because BigInteger is serializable, you can save it as a binary file once and greatly speed up your loading
Loading serialized objects should be faster than parsing a huge string at a time
Use objectoutputstream to save large integers and objectinputstream to read them back
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码