09-11
19
Java生成csv文件
作者:Java伴侣 日期:2009-11-19
复制内容到剪贴板 程序代码
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+"完成");
}
}
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+"完成");
}
}
评论: 0 | 引用: 0 | 查看次数: 957
发表评论