java 读取csv

java 读取csv

 

package com.vfsd;

import java.io.IOException;

import com.csvreader.CsvReader;

public class ReadCSVData {
    public static void main(String[] args) throws IOException {
        
        String h1[] = {"A","B","C","D","K","P"};
        readCSVAndWrite("F:\\PythonProject\\zzt\\week 1.csv");
    }
    
    /**
     * 读取CSV文件内容
     * @param csvFileName
     * @throws IOException
     */
    public static void readCSVAndWrite(String csvFileName) throws IOException{
        try {
            // 创建CSV读对象
            CsvReader csvReader = new CsvReader(csvFileName);
            // 读表头
            csvReader.readHeaders();
            
            String[] headers = csvReader.getHeaders();
            for(String indexStr:headers) {
                System.out.println(indexStr+" ");
            }
            
            
            while (csvReader.readRecord()){
                // 读一整行
                System.out.println(csvReader.getRawRecord());
                // 读这行的某一列
                //System.out.println(csvReader.get("A")+"\t"+csvReader.get("B")+"\t"+csvReader.get("C"));
            }
            csvReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

 

 

###########################

posted @ 2022-04-13 15:49  西北逍遥  阅读(1256)  评论(0编辑  收藏  举报