zoukankan      html  css  js  c++  java
  • java cvs

    CSV是逗号分隔文件(Comma Separated Values)的首字母英文缩写,是一种用来存储数据的纯文本格式,通常用于电子表格或数据库软件。

    1、CSV导入/导出封装类

    package com.parami.utils;
    
    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 林计钦
     * @version 1.0 Jan 27, 2014 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 {
                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("
    ");
                    }
                }
                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;
        }
    }

    2、CSV导入/导出测试

    package junit.test;
    
    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.junit.Test;
    
    import com.parami.utils.CSVUtils;
    
    /**
     * CSV操作(导出和导入)
     * 
     * @author 林计钦
     * @version 1.0 Jan 27, 2014 4:17:02 PM
     */
    public class CsvTest {
    
        /**
         * CSV导出
         * 
         * @throws Exception
         */
        @Test
        public void exportCsv() {
            List<String> dataList=new ArrayList<String>();
            dataList.add("1,张三,男");
            dataList.add("2,李四,男");
            dataList.add("3,小红,女");
            boolean isSuccess=CSVUtils.exportCsv(new File("D:/test/ljq.csv"), dataList);
            System.out.println(isSuccess);
        }
        
        /**
         * CSV导出
         * 
         * @throws Exception
         */
        @Test
        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);
                }
            }
        }
        
        
    }
  • 相关阅读:
    iOS开发快捷键
    通达信指标函数说明大全(2014)(转)
    windows下的wxWidgets环境配置
    Qt->数字格式化
    被称"硬盘杀手"的几个win7系统服务如何关闭(转)
    关闭系统索引(转)
    SSD固态硬盘优化(转)
    通达信的文件目录结构(转)
    Macdrive8破解版如何激活
    IOS6.0调用通讯录和之前的差别
  • 原文地址:https://www.cnblogs.com/tongBJ/p/7218472.html
Copyright © 2011-2022 走看看