我必须在磁盘文件中写入一系列字符,我想使用批量写入来减少磁盘 I/O。目前,我正在使用 File Writer 类。然而,它太慢了。任何人都可以帮助我如何在 Java 中执行批量写入,例如维护一个大缓冲区并定期刷新它。
最佳答案
您可能对 FileChannel
感兴趣. Channel
旨在执行批量 IO 操作,往返于 Buffer
例如:
FileChannel fileOut = new FileOutputStream(file).getChannel();
fileOut.write(ByteBuffer.wrap("Whatever you want to write".getBytes()));
关于Java 批量写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10065723/