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() <= 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?