Keep track of the number of bytes you’re writing, and when it reaches a specified point, close the existing file and continue in a new one. There’s no need to analyze the size of the file, since you know exactly what’s going into it.
Something like this:
long fileSizeByteLimit = 5000000;
long bytesOutput = 0;
while(THEREAREMORELINESTOOUTPUT) {
//Open a new file
while(bytesOutput <= fileSizeByteLimit) {
writer.append(lineOfOutput);
bytesOutput += lineOfOutput.length();
}
//Close file
}
1
solved How to write a String to fixed-size text files in Java? [closed]