CSV文件的导入和导出

  • Post author:
  • Post category:其他



导入导出CSV文件是一个较为常用的工具类,这里我就做下简单的总结,方便以后使用


废话不多说
废话不多说
废话不多说
这里写图片描述
这里写图片描述
直接上代码



CSV主要工具类

package com.example.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 韩哲
 */
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 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;
    }
}


Activity中的调用

    /**
     * CSV导出
     */
    public void exportCsv(List<Person> l) {
        String path = Environment.getExternalStorageDirectory().getPath();
        List<String> dataList = new ArrayList<String>();
        dataList.add("No,Name,Sex");
        for (int i = 0; i < l.size(); i++) {
            String msg = list.get(i).getSex() + "," + list.get(i).getName() + "," + list.get(i).getAge();
            dataList.add(msg);
        }
        dataList.add("1,张三,爷们");
        dataList.add("2,李四,爷们");
        dataList.add("3,王二麻子,爷们");
        boolean isSuccess = CSVUtils.exportCsv(new File(path + "/" + "yourfile.csv"), dataList);
        System.out.println(isSuccess);
    }

    /**
     * CSV导入
     * 
     * @throws Exception
     */
    public void importCsv() {
        List<String> dataList = CSVUtils.importCsv(new File("D:/test/ljq.csv"));
        if (dataList != null && !dataList.isEmpty()) {
            for (String data : dataList) {
                System.out.println(data);
            }
        }
    }


以上就是CSV文件的导入和导出了,比较简单的工具类,直接Copy到自己的工程即可,我就不附上Demo了

/



********************************************END*******************************************



/



版权声明:本文为qq_15700209原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。