java读取Csv格式文件
1.用BufferedReader方法来读取文件
2.spilt()函数以逗号为分割将整行分割,得到一个数组
3.为了后期方便调用函数,我用List进行存储,List<String[]>即为返回值
package 课设.readCsv;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ReadCsv {
/* 读取csv文件的方法
* 主要函数是ReadMethod()
* 放回类型是List<String[]> 一个List的集合 每一个元素是包含数条信息的数组
* (MsgID/CompanyId/BicycleNo/OrderId/DepartTime/DepartLongitude/DepartLatitude/ArriveTime/ArriveLongitude/ArriveLatitude/DBTime)
*/
List<String[]> ReadMethod(){
String csvFile = "Sharedbikezjut-sample.csv";
String line = "";
String cvsSplitBy = ",";
int count = 0;
String[] Line = new String[1];
List<String[]> BikeDataList = new ArrayList<String[]>();
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null) {
Line = line.split(cvsSplitBy);//将逗号作为分隔符 划分一行 得到数组[]
BikeDataList.add(Line);
// System.out.println("Order:"+(++count)+" Company:"+Line[1]+" DepartTime: "+Line[4]+ " ArriveTime:"+Line[7]);
}
} catch (IOException e) {
e.printStackTrace();
}
return BikeDataList;
}
public static void main(String[] args) {
new ReadCsv().ReadMethod();
}
}
跑出来的结果如下:
Order:29979 Company:hellobike DepartTime: 2018/3/20 9:38 ArriveTime:2018/3/20 9:43
Order:29980 Company:hellobike DepartTime: 2018/3/20 16:03 ArriveTime:2018/3/20 16:09
Order:29981 Company:hellobike DepartTime: 2018/3/20 16:18 ArriveTime:2018/3/20 16:34
Order:29982 Company:hellobike DepartTime: 2018/3/20 18:09 ArriveTime:2018/3/20 18:14
Order:29983 Company:hellobike DepartTime: 2018/3/20 15:26 ArriveTime:2018/3/20 15:27
Order:29984 Company:hellobike DepartTime: 2018/3/20 14:58 ArriveTime:2018/3/20 15:05
Order:29985 Company:hellobike DepartTime: 2018/3/20 11:38 ArriveTime:2018/3/20 11:45
Order:29986 Company:hellobike DepartTime: 2018/3/20 7:25 ArriveTime:2018/3/20 7:33
Order:29987 Company:hellobike DepartTime: 2018/3/20 7:37 ArriveTime:2018/3/20 7:46
Order:29988 Company:hellobike DepartTime: 2018/3/20 15:58 ArriveTime:2018/3/20 16:05
Order:29989 Company:hellobike DepartTime: 2018/3/20 13:04 ArriveTime:2018/3/20 13:09
Order:29990 Company:hellobike DepartTime: 2018/3/20 17:12 ArriveTime:2018/3/20 17:20
Order:29991 Company:hellobike DepartTime: 2018/3/20 11:38 ArriveTime:2018/3/20 11:55
Order:29992 Company:hellobike DepartTime: 2018/3/20 14:46 ArriveTime:2018/3/20 14:51
Order:29993 Company:hellobike DepartTime: 2018/3/20 9:45 ArriveTime:2018/3/20 9:54