Generate Random String and Writing to File my task is to generate random strings and store in a file till the size of file is less than 10MB. My approach towards this problem is as follows.

import java.io.File;
import java.io.FileWriter;

public class Application {
    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();
        File file = new File("Hello.txt");
        file.createNewFile();
        FileWriter writer = new FileWriter(file);

        while (file.length() &lt;= 1e+7) {
            writer.write(Generator.generateRandomString(12, Generator.Mode.ALPHANUMERIC));
            writer.write("\n");
            writer.write(Generator.generateRandomString(12, Generator.Mode.NUMERIC));
            writer.write("\n");
            writer.write(Generator.generateRandomString(12, Generator.Mode.ALPHA));
            writer.write("\n");
        }
        writer.flush();
        writer.close();
        long end = System.currentTimeMillis();
        System.out.println((end - start) / 1000f + " seconds");
    }
}


It takes about 100.495 seconds to complete on my i3 processor with 4GB of RAM. So my question is, how to improve this performance? Why is the performance so poor? Is there any another workaround for this?
It's the repeated calls to file.length() that is slowing you down.

Let's try this

while (file.length() &lt;= 1e+7) {
  writer.write("abcdefghijkl");
  writer.write("\n");
  writer.write("abcdefghijkl");
  writer.write("\n");
  writer.write("abcdefghijkl");
  writer.write("\n");
}


On my machine that took ~51s.

Now let's try this:

for (int length = 0; length &lt;= 1e+7; length += 39) {
  writer.write("abcdefghijkl");
  writer.write("\n");
  writer.write("abcdefghijkl");
  writer.write("\n");
  writer.write("abcdefghijkl");
  writer.write("\n");
}


Approximately 0.3s. And a quick sanity check:


$ du -h Hello.txt
9.6M    Hello.txt





There's a problem with the original method, which is the interaction of file.length() and using a buffered writer. Let's try to write up to 400 bytes:

while (file.length() &lt;= 400) {
  writer.write("abcdefghijkl");
  writer.write("\n");
}


We actually end up with a lot more because of the buffered writer:


$ du -b Hello.txt
8203    Hello.txt



A similar problem is also present in the method presented here: we end up writing 403 bytes, not because of buffering but because of how we're counting. If you want to be precise about the total file size, you will need to modify it.