I'm trying to print a very large BigInteger to a .txt file, but when the number reaches a certain size, it prints nothing. Code:
BigInteger bi = new BigInteger("16777216");
int exponent = 1000000;
bi = bi.pow(exponent);
String txtToPrint = bi.toString();
sendToFile(txtToPrint, "output.txt");
private static void sendToFile(String txtToPrint, String fileName) {
try {
FileWriter fileWriter = new FileWriter(fileName);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(txtToPrint);
bufferedWriter.close();
}
catch(IOException e) {
System.out.println("Error writing to file '" + fileName + "'");
}
}
Whenever the exponent is greater than 566 the output file is empty, instead of containing the number. The goal is to have an exponent of 1 000 000, or even greater.
I thought BigInteger did not have a size limit, so my question is: What limit am I exceeding and is there a way to solve this problem?
EDIT: When trying to flush and close the filewriter, I got this exception:
java.io.IOException: Stream closed
at sun.nio.cs.StreamEncoder.ensureOpen(Unknown Source)
at sun.nio.cs.StreamEncoder.flush(Unknown Source)
at java.io.OutputStreamWriter.flush(Unknown Source)
at PrintInt.main(PrintInt.java:4
EDIT: The problem only occurs when running the program in Eclipse, I tried exporting it to an external jar, and everything worked just fine. I am using Eclipse Mars.1 Release (4.5.1) and Java jre1.8.0_131 and Cp1252 encoding.