Java-读取具有各种数据类型的CSV文件的最有效方法 [英] Java - Most efficient way to read in a CSV file with various data types
问题描述
我正在尝试读取具有各种数据类型的CSV文件.
I'm attempting to read in a CSV file with various data types.
工作表的一行如下所示:
A row of the sheet would be like below:
单月,加速,约翰,史密斯(John/Smith),08/15/1951,是
然后我需要为每个字段分配一个变量名称,进行一些计算,打印输出,然后移到excel工作表的下一行
I then need to assign each field to a variable name, preform some calculations, print an output and then move onto the next line in the excel sheet
到目前为止,我一直在使用以下
Up until now, I've been using the below
ArrayList<String> lines = new ArrayList<>();
try {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line = null;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
但是这会创建一个数组,每个插槽包含一个长字符串,该字符串带有相应excel行的文本(包括逗号). 这似乎效率低下且不切实际,因为我随后必须遍历每个插槽/字符串以提取值.
But this creates an array with each slot containing a long string with the text (including comma) of the corresponding excel row. This seems inefficient and impractical as i then have to traverse each slot/string to extract the values.
一旦有了方法论,我就不会在编写代码时遇到任何问题,但是我不知道最好的解决方法
Once I have the methodology, I wont have any issue writing the code but i don't know the best way to go about it
最好分别读取每个单元格并将其分配给一个变量吗? 还是一次读取一个文件然后再遍历是更好的选择? 也许有一种更有效的方式完成这项任务
Is it better to read each cell separately and assign to a variable ? Or is it better to read in a file once and traverse it afterwards? Perhaps there is a more efficient way to do this task
我也尝试将整个CSV文件作为2D数组读取,但是不同的数据类型可能是个问题.
Edit : I also though of attempting to read in the entire CSV file as a 2D array, but the different data types could be an issue..?
推荐答案
您可以尝试类似的操作.使用StringTokenizer用逗号分隔行,并在每次迭代中将这些元素作为字符串添加到另一个List中.
You can try something similar to this. Use StringTokenizer to split the line by comma and add those elements to another List as strings in each iteration.
ArrayList<ArrayList<String>> lines = new ArrayList<>();
try {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line = null;
while ((line = reader.readLine()) != null) {
ArrayList<String> tokens = new ArrayList<>();
StringTokenizer st = new StringTokenizer(line, ",");
while (st2.hasMoreElements()) {
tokens.add(st2.nextElement());
}
lines.add(tokens);
}
}
现在,您可以使用适当的强制转换将它们转换为所需的类型.例如,要获取日期,
Now you can use proper casts to convert them to types you want. For example, to get the date,
DateFormat format = new SimpleDateFormat("mm/dd/yyyy", Locale.ENGLISH);
String dateString = lines.get(0).get(5);
Date date = format.parse(dateString);
这篇关于Java-读取具有各种数据类型的CSV文件的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!