zoukankan      html  css  js  c++  java
  • CSV导出

    CSV 导入导出工具类

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * CSV操作(导出和导入)
     *
     * @author wjm
     * @version 1.0 Nov 24, 2015 4:30:58 PM
     */
    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 {
                // OutputStreamWriter in_=new OutputStreamWriter(new
                // FileOutputStream("文件名"), "gbk");
                out = new FileOutputStream(file);
                osw = new OutputStreamWriter(out, "gbk");
                bw = new BufferedWriter(osw);
                if (dataList != null && !dataList.isEmpty()) {
                    for (String data : dataList) {
                        bw.append(data).append("
    ");
                    }
                }
                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 List<String> importCsv(File file) {
            List<String> dataList = new ArrayList<String>();
    
            BufferedReader br = null;
            try {
                br = new BufferedReader(new FileReader(file));
                String line = "";
                while ((line = br.readLine()) != null) {
                    dataList.add(line);
                }
            } catch (Exception e) {
            } finally {
                if (br != null) {
                    try {
                        br.close();
                        br = null;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    
            return dataList;
        }
    }
  • 相关阅读:
    linux中配置celery定时任务
    django2 将request.body 中的json字符串转换成字典
    在postman中各种填写参数的区别
    requests.Request 中参数data与json的区别
    Java中使用OpenSSL生成的RSA公私钥进行数据加解密
    openssl生成RSA密钥证书
    WKWebViewJavascriptBridge
    LeetCode实现 strStr()Swift
    LeetCode删除排序数组中的重复项Swift
    LeetCode合并两个有序链表Swift
  • 原文地址:https://www.cnblogs.com/kongxianghao/p/8569535.html
Copyright © 2011-2022 走看看