Fastest way to write to file?

后端 未结 6 1886
难免孤独
难免孤独 2020-12-02 15:54

I made a method that takes a File and a String. It replaces the file with a new file with that string as its contents.

This is what I made

6条回答
  •  [愿得一人]
    2020-12-02 16:14

    A simple test for you

    char[] chars = new char[100*1024*1024];
    Arrays.fill(chars, 'A');
    String text = new String(chars);
    long start = System.nanoTime();
    BufferedWriter bw = new BufferedWriter(new FileWriter("/tmp/a.txt"));
    bw.write(text);
    bw.close();
    long time = System.nanoTime() - start;
    System.out.println("Wrote " + chars.length*1000L/time+" MB/s.");
    

    Prints

    Wrote 135 MB/s.
    

提交回复
热议问题