在文本文件Java中写入大量数据的最快方法
我必须在text [csv]文件中写入大量数据。我使用BufferedWriter写入数据,并且花费了大约40秒的时间来写入174 mb的数据。这是Java可以提供的最快速度吗?
bufferedWriter = new BufferedWriter ( new FileWriter ( "fileName.csv" ) );
注意:这40秒还包括从结果集中迭代和获取记录的时间。:) 174 mb用于结果集中的400000行。
回答:
你可以尝试删除BufferedWriter并直接使用FileWriter。在现代系统上,无论如何,你很有可能只是写入驱动器的缓存。
我需要4-5秒的时间来写入175MB(400万个字符串)-这是在运行Windows XP和80GB,7200-RPM日立磁盘的双核2.4GHz戴尔上进行的。
你能否确定记录检索有多少时间和文件写入有多少时间?
import java.io.BufferedWriter;import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
public class FileWritingPerfTest {
private static final int ITERATIONS = 5;
private static final double MEG = (Math.pow(1024, 2));
private static final int RECORD_COUNT = 4000000;
private static final String RECORD = "Help I am trapped in a fortune cookie factory\n";
private static final int RECSIZE = RECORD.getBytes().length;
public static void main(String[] args) throws Exception {
List<String> records = new ArrayList<String>(RECORD_COUNT);
int size = 0;
for (int i = 0; i < RECORD_COUNT; i++) {
records.add(RECORD);
size += RECSIZE;
}
System.out.println(records.size() + " 'records'");
System.out.println(size / MEG + " MB");
for (int i = 0; i < ITERATIONS; i++) {
System.out.println("\nIteration " + i);
writeRaw(records);
writeBuffered(records, 8192);
writeBuffered(records, (int) MEG);
writeBuffered(records, 4 * (int) MEG);
}
}
private static void writeRaw(List<String> records) throws IOException {
File file = File.createTempFile("foo", ".txt");
try {
FileWriter writer = new FileWriter(file);
System.out.print("Writing raw... ");
write(records, writer);
} finally {
// comment this out if you want to inspect the files afterward
file.delete();
}
}
private static void writeBuffered(List<String> records, int bufSize) throws IOException {
File file = File.createTempFile("foo", ".txt");
try {
FileWriter writer = new FileWriter(file);
BufferedWriter bufferedWriter = new BufferedWriter(writer, bufSize);
System.out.print("Writing buffered (buffer size: " + bufSize + ")... ");
write(records, bufferedWriter);
} finally {
// comment this out if you want to inspect the files afterward
file.delete();
}
}
private static void write(List<String> records, Writer writer) throws IOException {
long start = System.currentTimeMillis();
for (String record: records) {
writer.write(record);
}
writer.flush();
writer.close();
long end = System.currentTimeMillis();
System.out.println((end - start) / 1000f + " seconds");
}
}
以上是 在文本文件Java中写入大量数据的最快方法 的全部内容, 来源链接: utcz.com/qa/434724.html