Java创建csv文件

package com.vefan.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+"完成");
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值