【发布时间】:2014-06-19 11:59:47
【问题描述】:
我想将 20000 到 50000 记录文件之类的大型 csv 文件转换为 json 数组,但转换需要将近 1 分钟,有什么方法可以在不到 5 秒的时间内实现它。
ResourceBundle rb = ResourceBundle.getBundle("settings");
String path = rb.getString("fileandfolder.Path");
System.out.println(path + "ssdd");
String csvPath = request.getParameter("DP") != null ? request
.getParameter("DP").toString() : "";
String orname = path + csvPath;
File file = new File(orname);
FileReader fin = new FileReader(file); //Read file one by one
BufferedReader bi = new BufferedReader(fin);
int res;
String csv = "";
while ((res = fin.read()) != -1) {
csv = csv + ((char) res); //Converted int to char and stored in csv
}
long start3 = System.nanoTime();
JSONArray array = CDL.toJSONArray(csv);
String Csvs = array.toString();
long time3 = System.nanoTime() - start3;
System.out
.printf("Took %.3f seconds to convert to a %d MB file, rate: %.1f MB/s%n",
time3 / 1e9, file.length() >> 20, file.length()
* 1000.0 / time3);
【问题讨论】:
-
Cvs 库..???你能告诉我任何例子吗?
-
我已将其从不再需要的代码中删除。
-
然后更改您的代码以反映这一点,您的代码不会按原样编译。所以
long time3 = System.nanoTime() - start2;应该是long time3 = System.nanoTime() - start3; -
请注意,通过提供大量没有计时的不必要的文件读取代码(显然),您在这里混淆了很多人。
-
一个网络响应中的 50k 个条目无论如何听起来都是个坏主意。