java读取csv文件并修改写入另一个文件

csv文件为数据库导出的
代码:

package com.ips.pbcs.base;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

import com.ips.pbcs.base.util.EncryptUtil;

public class Test {
	public static void main(String[] args) {
		try {
			System.out.println("start");
			//写入文件
			File csv = new File("C:\\Users\\User\\Desktop\\testabc\\testabc-1.csv"); // 写入CSV数据文件
			BufferedWriter bw = new BufferedWriter(new FileWriter(csv, true)); // 附加
			bw.write("\"BANK_ACCOUNT\",\"BANK_ACCOUNT_NAME\",\"SUPPLIER_NAME\",\"SUB_IN_SUPPLIER_NAME\",\"FINISH_TIME\",\"OUT_AMOUNT\",\"MER_CODE_POS\",\"MER_CODE\"");
			bw.newLine();
			
			//读取文件
			BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\User\\Desktop\\testabc\\testabc.csv"));// 换成你的文件名
			reader.readLine();// 第一行信息,为标题信息,不用,如果需要,注释掉
			String line = null;
			while ((line = reader.readLine()) != null) {
				String item[] = line.split(",");// CSV格式文件为逗号分隔符文件,这里根据逗号切分
				//解密第一列
				String bankAccount = item[0].substring(1, item[0].length()-1); //去掉双引号
				String str1 = EncryptUtil.decryptInfo(bankAccount, EncryptUtil.ENCRYPT_TYPE_AES128);
				String bankAccountName = item[1].substring(1, item[1].length()-1);
				String str2 = EncryptUtil.decryptInfo(bankAccountName, EncryptUtil.ENCRYPT_TYPE_AES128);
				String supplierName = item[2].substring(1, item[2].length()-1);
				String subInSupplierName = item[3].substring(1, item[3].length()-1);
				String finishTime = item[4].substring(1, item[4].length()-1);
				String outAmount = item[5].substring(1, item[5].length()-1);
				String merCodePos = item[6].substring(1, item[6].length()-1);
				String merCode = item[7].substring(1, item[7].length()-1);
				String value = "\""+str1+"\","+"\""+str2+"\","+"\""+supplierName+"\","+"\""+subInSupplierName+"\","+"\""+finishTime+"\","+"\""+outAmount+"\","+"\""+merCodePos+"\","+"\""+merCode+"\"";
				System.out.println(value);
				bw.write(value);
				bw.newLine();
			}
			reader.close();
			bw.close();
			System.out.println("end");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

猜你喜欢

转载自blog.csdn.net/csj50/article/details/110294244