Csv工具类

Csv工具类

import com.csvreader.CsvReader;
import com.soft.backstage.alipay.bean.AlipayBill;
import com.soft.backstage.alipay.bean.AlipayDzdInfo;
import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * Csv文件读取工具类
 */
public class CsvUtils {

    /**
     * 导出
     *
     * @param file     csv文件(路径+文件名),csv文件不存在会自动创建
     * @param dataList 数据
     * @return
     */
    public static boolean exportCsv(File file, List<String> dataList) {
        boolean isSucess = false;
        FileOutputStream out = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            out = new FileOutputStream(file);
            osw = new OutputStreamWriter(out);
            bw = new BufferedWriter(osw);
            if (dataList != null && !dataList.isEmpty()) {
                for (String data : dataList) {
                    bw.append(data).append("\r");
                }
            }
            isSucess = true;
        } catch (Exception e) {
            isSucess = false;
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                    bw = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (osw != null) {
                try {
                    osw.close();
                    osw = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                    out = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return isSucess;
    }

    /**
     * 导入
     *
     * @param file csv文件(路径+文件)
     * @return
     */
    public static boolean importCsv(String file, int page) {
        List<String[]> csvList = new ArrayList<String[]>();
        boolean result = false;
        int index = 0;
        int strat = page * 5000;
        int end = (page + 1) * 5000;
        try {
            CsvReader csvReader = new CsvReader(file, ',', Charset.forName("GBK"));
            // 读表头
            csvReader.readHeaders();
            while (csvReader.readRecord()) {
                if ((index >= strat) && (index < end)) {
                    // 读一整行
                    System.out.println(csvReader.getRawRecord());
                    csvList.add(csvReader.getValues());
                }
                if (index == (end - 1)) {
                    result = true;
                    break;
                }
                index++;
            }
            csvReader.close();
        } catch (IOException e) {
            System.out.println(e);
        }
        List<AlipayBill> list = new ArrayList();
        //遍历读取的CSV文件
        for (int row = 0; row < csvList.size(); row++) {
            AlipayBill alipayBill = new AlipayBill();
            // 取得第row行第0列的数据
            if (csvList.get(row).length == 25) {
//                alipayBill.setTradeNo(csvList.get(row)[0]);
//                alipayBill.setOutTradeNo(csvList.get(row)[1]);
                list.add(alipayBill);
            }
        }

        System.out.println("读取结束!" + page + ":" + index);
        return result;
    }


    public static void writeContent(List<AlipayDzdInfo> list, String pathName) { //导出百万条数据name是文件名  list是数据
        try {
            writeFileContent(list,pathName); //writeFileContent()这个方法是分批往csv文件中追加数据  ,下面注释掉的是一次性往csv文件中写 数据
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static boolean writeFileContent( List<AlipayDzdInfo> list, String filepath) throws IOException {
        Boolean bool = false;
        String temp = "";
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        FileOutputStream fos = null;
        PrintWriter pw = null;
        try {
            File file = new File(filepath);//文件路径(包括文件名称)
            //将文件读入输入流
            fis = new FileInputStream(file);
            isr = new InputStreamReader(fis);
            br = new BufferedReader(isr);
            StringBuffer buffer = new StringBuffer();
            int i;
            //文件原有内容
            for (i = 0; (temp = br.readLine()) != null; i++) {
                buffer.append(temp);
                // 行与行之间的分隔符 相当于“\n”
                buffer = buffer.append(System.getProperty("line.separator"));
            }
            for (int q = 0; q < list.size(); q++) {
                buffer.append(list.get(q).getYzpzxh() + ",");
                buffer.append(list.get(q).getJyje() + ",");
                buffer.append(list.get(q).getSfzh() + ",");
                buffer.append(list.get(q).getJfrxm() + ",");
                buffer.append(list.get(q).getGldm() + ",");
                buffer.append(list.get(q).getXzlx() + " \r\n");
            }
            fos = new FileOutputStream(file);
            pw = new PrintWriter(fos);
            pw.write(buffer.toString().toCharArray());
            pw.flush();
            bool = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //不要忘记关闭
            if (pw != null) {
                pw.close();
            }
            if (fos != null) {
                fos.close();
            }
            if (br != null) {
                br.close();
            }
            if (isr != null) {
                isr.close();
            }
            if (fis != null) {
                fis.close();
            }
        }
        return bool;
    }



    public static boolean writeFileTitle(AlipayDzdInfo alipayDzdInfo,String filepath) throws IOException {
        Boolean bool = false;
        String temp = "";
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        FileOutputStream fos = null;
        PrintWriter pw = null;
        try {
            File file = new File(filepath);//文件路径(包括文件名称)
            //将文件读入输入流
            fis = new FileInputStream(file);
            isr = new InputStreamReader(fis);
            br = new BufferedReader(isr);
            StringBuffer buffer = new StringBuffer();
            int i;
            //文件原有内容
            for (i = 0; (temp = br.readLine()) != null; i++) {
                buffer.append(temp);
                // 行与行之间的分隔符 相当于“\n”
                buffer = buffer.append(System.getProperty("line.separator"));
            }
            buffer.append(alipayDzdInfo.getAllMoney() + "\r\n");
            buffer.append(alipayDzdInfo.getAllNumber() + "\r\n");
            fos = new FileOutputStream(file);
            pw = new PrintWriter(fos);
            pw.write(buffer.toString().toCharArray());
            pw.flush();
            bool = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //不要忘记关闭
            if (pw != null) {
                pw.close();
            }
            if (fos != null) {
                fos.close();
            }
            if (br != null) {
                br.close();
            }
            if (isr != null) {
                isr.close();
            }
            if (fis != null) {
                fis.close();
            }
        }
        return bool;
    }

}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值