Generate Random String and Writing to File
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.