09-11
19

Java生成csv文件

import java.io.ByteArrayOutputStream;  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.FileWriter;  
import java.io.IOException;  
import java.nio.channels.FileChannel;  
import java.util.ArrayList;  
import java.util.Iterator;  
import java.util.List;  
  
import com.vefan.common.util.FileUtils;  
  
public class CsvWriter  
{  
  
    public static void main(String args[]){  
        CsvWriter cw = new CsvWriter();  
        String csvFile = "E:/workspace/JavaApp/csv/createCSV.csv";  
        cw.createCSV(csvFile);  
          
        String localPath = "E:/workspace/JavaApp/csv/";  
        String fName = "createCsvByList.csv";  
        String sTitle = "ID,NAME,SEX,EMAIL,TEL";  
        List listSource = new ArrayList();  
        listSource.add("1,tomcat,male,tomcat@tomcat.com,1383838438");  
        listSource.add("2,Jboss,male,jboss@jboss.com,1484848748");  
          
        cw.createCSVByList(listSource, sTitle, localPath, fName);  
    }  
      
    /**  
     * 复制csv文件  
     * @param source 源文件  
     * @param dest   目标文件  
     * */  
    public void copyCSV(String source, String dest){  
        try  
        {  
            FileChannel in = new FileInputStream(source).getChannel();  
            FileChannel out = new FileOutputStream(dest).getChannel();  
              
//          in.transferTo(0, in.size(), out);  
            out.transferFrom(in, 0, in.size());  
              
            in.close();  
            out.close();  
        } catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
    }  
      
    /**  
     * 创建csv文件  
     * @param csvFile csv完整文件名  
     * */  
    public void createCSV(String csvFile){  
        FileWriter fw = null;  
        try  
        {  
            fw = new FileWriter(csvFile);  
            fw.write("ID,CODE,NAME,EMAIL,TEL\r\n");  
            fw.write("1,system,上将,vefan@hotmail.com,15935785489\r\n");  
            fw.write("2,sysman,中尉,vefan@hotmail.com,15687596324\r\n");  
            fw.write("3,sys,少尉,vefan@hotmail.com,15826589752\r\n");  
              
            fw.flush();  
            fw.close();  
        } catch (IOException e)  
        {  
            e.printStackTrace();  
        }finally{  
            if(null != fw){  
                try  
                {  
                    fw.close();  
                } catch (IOException e)  
                {  
                    e.printStackTrace();  
                }  
            }  
        }  
          
    }  
      
    /**  
     * 创建csv文件  
     * @param listSource 行数据  
     * @param sTitle     字段名  
     * @param localPath  目录路径  
     * @param fName      文件名  
     * */  
    public void createCSVByList(List listSource, String sTitle, String localPath, String fName){  
        ByteArrayOutputStream out = new ByteArrayOutputStream();  
        FileOutputStream fos = null;  
        try  
        {  
            out.write(sTitle.getBytes());  
            out.write(",".getBytes());  
            out.write("\n".getBytes());  
              
            Iterator it = listSource.iterator();  
            while(it.hasNext()){  
                String value = (String)it.next();  
                out.write(value.getBytes());  
                out.write(",".getBytes());    //以逗号为分隔符  
                out.write("\n".getBytes());   //换行  
            }  
            //没有目录,先生成目录  
            FileUtils fileTool = new FileUtils();  
            fileTool.newFolder(localPath);  
              
            File newfile = new File(localPath,fName);  
              
            fos = new FileOutputStream(newfile);  
            fos.write(out.toByteArray());  
              
            fos.flush();  
            out.close();  
            fos.close();  
        } catch (IOException e)  
        {  
            e.printStackTrace();  
        }finally{  
            try  
            {  
                if(null != out) out.close();  
                if(null != fos) fos.close();  
            } catch (IOException e)  
            {  
                e.printStackTrace();  
            }  
        }  
        System.out.println("生成"+fName+"完成");  
    }  
      
}  


文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: csv 生成
相关日志:
评论: 0 | 引用: 0 | 查看次数: 957
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭