如何用NIO提高写文件的速度?
想实现如下流程:
首先从db中select出一些数据,现在想把数据都写进文件。但是由于数据量太大,整个过程都很慢。
如何提高写文件的速度?据说java.nio可以很明显的提高读写文件的速度。于是写了如下代码:
假设数据都已经读取到ResultSet rs 中:
FileOutputStream fout = new FileOutputStream("C:\\test.txt"); FileChannel fcout = fout.getChannel(); try { while (rs.next()) { buffer.clear(); buffer.put((rs.getLong("ID") + "\t" + rs.getLong("NAME") + "\r\n") .getBytes()); buffer.flip(); fcout.write(buffer); } } catch ......