csv 是一种纯文本文件,可以用excel格式打开,也可以用文本打开。如下:
demo.csv.png
demo.csv.png
从文本打开的方式中可以看到csv是用逗号(,)隔开的,所以在读取的时候可以以逗号为标志分割。
下面是读取方式:
public class ReadCSV {
public static void main(String[] args) {
String fileName = "D:\\demo\\demo.csv";
readCSV(fileName);
}
public static void readCSV(String fileName){
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileName)), "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null){
System.out.println(line.toString());
String[] data = line.split(",");
for (int i = 0; i < data.length; i++) {
System.out.println(data[i]);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
网友评论