Fastest way to write to file?

后端 未结 6 1887
难免孤独
难免孤独 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:08

    Try using memory mapped files:

    FileChannel rwChannel = new RandomAccessFile("textfile.txt", "rw").getChannel();
    ByteBuffer wrBuf = rwChannel.map(FileChannel.MapMode.READ_WRITE, 0, textToSave.length());
    
    wrBuf.put(textToSave.getBytes());
    
    rwChannel.close();
    

提交回复
热议问题