CSV文件的读取和生成

摘要:本文为csv文件读取后再生成新的csv文件的demo

1.工程结构

2.原csv文件存放路径:C:/Users/Lenovo/Desktop/demo.csv,csv文件格式见下图

3.实体类

 

package entity;

public class DemoEntity {
	
	private String one;
	private String two;
	private String three;
	private String four;
	private String five;
	private String six;
	
	public String getOne() {
		return one;
	}
	public void setOne(String one) {
		this.one = one;
	}
	public String getTwo() {
		return two;
	}
	public void setTwo(String two) {
		this.two = two;
	}
	public String getThree() {
		return three;
	}
	public void setThree(String three) {
		this.three = three;
	}
	public String getFour() {
		return four;
	}
	public void setFour(String four) {
		this.four = four;
	}
	public String getFive() {
		return five;
	}
	public void setFive(String five) {
		this.five = five;
	}
	public String getSix() {
		return six;
	}
	public void setSix(String six) {
		this.six = six;
	}
	public DemoEntity(String one, String two, String three, String four, String five, String six) {
		super();
		this.one = one;
		this.two = two;
		this.three = three;
		this.four = four;
		this.five = five;
		this.six = six;
	}
	public DemoEntity() {
		super();
	}
	public String toRow() {
		return String.format("%s,%s,%s,%s,%s,%s", this.one,this.two,this.three,this.four,this.five,this.six);
	}
}

4.读取csv文件和生成的工具类

package utils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import entity.DemoEntity;

public class CsvUtil {

	/**
	 * 读取原csv文件更改格式
	 * 
	 * @throws IOException
	 */
	public static List<DemoEntity> readBaseCSV(String fileUrl) throws IOException {
		List<DemoEntity> demoEntityList = new ArrayList<DemoEntity>();
		File file = new File(fileUrl);
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
		// 跳过csv文件第一行
		bufferedReader.readLine();
		String line = null;
		while ((line = bufferedReader.readLine()) != null) {
			// 数据行
			String[] items = line.split(",");
			DemoEntity demoEntity = new DemoEntity();
			demoEntity.setOne(items[0]);
			demoEntity.setTwo(items[1]);
			demoEntity.setThree(items[2]);
			demoEntity.setFour(items[3]);
			demoEntity.setFive(items[4]);
			demoEntity.setSix("0");
			demoEntityList.add(demoEntity);
		}
		if (bufferedReader != null) {
			bufferedReader.close();
		}
		return demoEntityList;
	}

	/**
	 * 生成csv文件
	 */
	public static void writeNewCSV(String fileUrl, List<DemoEntity> demoEntityList) throws IOException {
		String filePath = "E:/CSVFile";
		String[] fileName = fileUrl.split("/");
		File file = new File(filePath + "/" + fileName[fileName.length - 1]);
		File parentFile = file.getParentFile();
		// 如果父文件夹不存在 创建
		if (parentFile != null && !parentFile.exists()) {
			parentFile.mkdir();
		}
		BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
		bufferedWriter.write("1,2,3,4,5,6");
		bufferedWriter.newLine();
		for (DemoEntity demoEntity : demoEntityList) {
			bufferedWriter.write(demoEntity.toRow());
			bufferedWriter.newLine();
		}
		bufferedWriter.flush();
		if (bufferedWriter != null) {
			bufferedWriter.close();
		}
	}
}

5.main方法调用

package start;

import java.io.IOException;

import utils.CsvUtil;

public class StartUp {

	public static void main(String[] args) throws IOException {
		String fileUrl = "C:/Users/Lenovo/Desktop/demo.csv";
		CsvUtil.writeNewCSV(fileUrl,CsvUtil.readBaseCSV(fileUrl));
	}

}

6.生成后的csv格式

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值