它用7秒鐘不寫。
我不能想像爲什麼CSVWriter是如此緩慢,除非它需要緩衝。
你可以嘗試
CSVWriter writer = new CSVWriter(
new BufferedWriter(new FileWriter("C:/csv/Sample.csv")), ';');
,並添加
writer.close();
或使用Java 7+
try(CSVWriter writer = ...) {
試試這個
import java.io.*;
public class DumbCSVWriter {
private final Writer writer;
private final String sep;
public DumbCSVWriter(Writer writer, String sep) {
this.sep = sep;
this.writer = writer instanceof BufferedWriter ? writer : new BufferedWriter(writer);
}
public void addRow(Object... values) throws IOException {
for (int i = 0; i < values.length - 1; i++) {
print(values[i]);
writer.write(sep);
}
if (values.length > 0)
print(values[values.length - 1]);
writer.write("\n");
}
private void print(Object value) throws IOException {
if (value == null) return;
String str = value.toString();
if (str.contains(sep) || str.contains("\"") || str.contains("\n")) {
str = '"' + str.replaceAll("\"", "\"\"");
}
writer.write(str);
}
public static void main(String[] args) throws IOException {
long start = System.nanoTime();
File file = new File("/tmp/deleteme");
DumbCSVWriter writer = new DumbCSVWriter(new FileWriter(file), ";");
String[] words = "hello,,has a;semi-colon,has a \"quote".split(",");
for (int i = 0; file.length() < 1024 * 1024; i++) {
writer.addRow(words);
}
writer.close();
long time = System.nanoTime() - start;
System.out.printf("Time tow rite 1 MB %.3f%n", time/1e9);
}
private void close() throws IOException {
writer.close();
}
}
個
打印
時間寫1 MB 0.307
你可以分享csvwriter的代碼? – Hirak
提取數據的可能性更大。提取所有數據需要多長時間,而不需要編寫它? –
@Hirak CSVWriter是OpenCSV jar中的一個類。這不是我定製的課程。 – Samurai