Java(77):Java读取和写入CSV文件工具类(list<list>)

Java读取和写入CSV文件工具类(list<list>)

备注:从list<list<String>>写入,读取到list<list<String>>

工具类CsvUtil.java

package com.xxx.utils;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author : HMF
 * @ClassName CsvUtil
 * @description 读取和写入csv的工具类
 * @date: 2022/2/25 21:37
 **/


public class CsvUtil {
    /**
     * 读取CSV格式的文档数据
     * @param filePath CSV格式的文件路劲
     * @return dataList csv数据读取放入二维list中。
     */
    public static List<List<String>> readCSVFileData(String filePath){
        BufferedReader reader=null;
        List<List<String>> dataList=new ArrayList<>();
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"));
        }catch(FileNotFoundException | UnsupportedEncodingException e){
            e.printStackTrace();
        }
        try{
            String line=null;
            while ((line=reader.readLine())!=null){
                String aa[]=line.split(",");
                List<String> cellList= Arrays.asList(aa);
                //System.out.println(cellList);
                dataList.add(cellList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                if (reader != null) {
                    //释放资源
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
            return dataList;
    }
    /**
     * 读取CSV格式的文档数据
     * @param filePath CSV格式的文件路劲
     * @param dataList 需要写入的数据
     * @return dataList csv数据读取放入二维list中。
     */
    public static void writeCSVFileData(String filePath,List<List<String>> dataList){
        BufferedWriter bw=null;
        try {
            bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath, true), "UTF-8"));
            for(int i = 0; i<dataList.size(); i++) {
                bw.write(dataList.get(i).get(0)+","+dataList.get(i).get(1));
                bw.newLine();
            }
        } catch (FileNotFoundException e) {
            //e.printStackTrace();
            System.out.println(e);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                if (bw != null) {
                    bw.flush();
                    //释放资源
                    bw.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

调用

import java.util.ArrayList;
import java.util.List;
import com.xxx.utils.CsvUtil;
/**
 * @author : HMF
 * @ClassName ReadFileTest
 * @description TODO
 * @date: 2022/2/25 21:37
 **/


public class ReadFileTest {
    public static void main(String[] args) {
        List<List<String>> dataList2=new ArrayList<>();
        List<String> a1=new ArrayList<>();
        a1.add("序号");
        a1.add("姓名");
        dataList2.add(a1);
        List<String> a2=new ArrayList<>();
        a2.add("1");
        a2.add("张三");
        dataList2.add(a2);
        List<String> a3=new ArrayList<>();
        a3.add("2");
        a3.add("李四");
        dataList2.add(a3);
        CsvUtil.writeCSVFileData("./test.csv",dataList2);

        //读取
        List<List<String>> dataList=CsvUtil.readCSVFileData("./test.csv");
        for(int i = 0; i<dataList.size(); i++){
            if(i !=0) {
                //System.out.println(dataList.get(i));
                String id=dataList.get(i).get(0);
                String name=dataList.get(i).get(1);
                System.out.println(id+" "+name);
            }
        }
    }
}

执行结果

list参考:Java中List集合的常用方法

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宁宁可可

您的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值