一、java读写csv文件
1、说在前面
本文主要是借助 opencsv-5.4.jar工具包,来实现java对csv文件的读取,并将读取的结果,存放到一个list集合中,然后再写入到一个新的csv文件中。
2、准备工作
(1)opencsv-5.4.jar工具jar包,可以下载 (2)实体类 Domain,该类的核心之处就是要加toString()方法,因为后面解析的时候需要用到
package zgw.pojo; public class Domain { public String scqy; public String cpdl; public String cpxl; public String cpph; public String xqjhsl; public String qdck; public String dccf; public String ckmax; public String jhsj; public String drpcbh; public String ckl; public String rkl; public String qckc; public String ztkc; public String kskcl; public String yswf; public String rksj; public String cksj; public String ztcpyqddq; public String kccz; public String getKccz() { return kccz; } public void setKccz(String kccz) { this.kccz = kccz; } public String getScqy() { return scqy; } public void setScqy(String scqy) { this.scqy = scqy; } public String getCpdl() { return cpdl; } public void setCpdl(String cpdl) { this.cpdl = cpdl; } public String getCpxl() { return cpxl; } public void setCpxl(String cpxl) { this.cpxl = cpxl; } public String getCpph() { return cpph; } public void setCpph(String cpph) { this.cpph = cpph; } public String getXqjhsl() { return xqjhsl; } public void setXqjhsl(String xqjhsl) { this.xqjhsl = xqjhsl; } public String getQdck() { return qdck; } public void setQdck(String qdck) { this.qdck = qdck; } public String getDccf() { return dccf; } public void setDccf(String dccf) { this.dccf = dccf; } public String getCkmax() { return ckmax; } public void setCkmax(String ckmax) { this.ckmax = ckmax; } public String getJhsj() { return jhsj; } public void setJhsj(String jhsj) { this.jhsj = jhsj; } public String getDrpcbh() { return drpcbh; } public void setDrpcbh(String drpcbh) { this.drpcbh = drpcbh; } public String getCkl() { return ckl; } public void setCkl(String ckl) { this.ckl = ckl; } public String getRkl() { return rkl; } public void setRkl(String rkl) { this.rkl = rkl; } public String getQckc() { return qckc; } public void setQckc(String qckc) { this.qckc = qckc; } public String getZtkc() { return ztkc; } public void setZtkc(String ztkc) { this.ztkc = ztkc; } public String getKskcl() { return kskcl; } public void setKskcl(String kskcl) { this.kskcl = kskcl; } public String getYswf() { return yswf; } public void setYswf(String yswf) { this.yswf = yswf; } public String getRksj() { return rksj; } public void setRksj(String rksj) { this.rksj = rksj; } public String getCksj() { return cksj; } public void setCksj(String cksj) { this.cksj = cksj; } public String getZtcpyqddq() { return ztcpyqddq; } public void setZtcpyqddq(String ztcpyqddq) { this.ztcpyqddq = ztcpyqddq; } public Domain() { } public Domain(String scqy, String cpdl, String cpxl, String cpph, String xqjhsl, String qdck, String dccf, String ckmax, String jhsj, String drpcbh, String ckl, String rkl, String qckc, String ztkc, String kskcl, String yswf, String rksj, String cksj, String ztcpyqddq,String kccz) { this.scqy = scqy; this.cpdl = cpdl; this.cpxl = cpxl; this.cpph = cpph; this.xqjhsl = xqjhsl; this.qdck = qdck; this.dccf = dccf; this.ckmax = ckmax; this.jhsj = jhsj; this.drpcbh = drpcbh; this.ckl = ckl; this.rkl = rkl; this.qckc = qckc; this.ztkc = ztkc; this.kskcl = kskcl; this.yswf = yswf; this.rksj = rksj; this.cksj = cksj; this.ztcpyqddq = ztcpyqddq; this.kccz = kccz; } @Override public String toString() { return "Domain{" + "scqy='" + scqy + '\'' + ", cpdl='" + cpdl + '\'' + ", cpxl='" + cpxl + '\'' + ", cpph='" + cpph + '\'' + ", xqjhsl='" + xqjhsl + '\'' + ", qdck='" + qdck + '\'' + ", dccf='" + dccf + '\'' + ", ckmax='" + ckmax + '\'' + ", jhsj='" + jhsj + '\'' + ", drpcbh='" + drpcbh + '\'' + ", ckl='" + ckl + '\'' + ", rkl='" + rkl + '\'' + ", qckc='" + qckc + '\'' + ", ztkc='" + ztkc + '\'' + ", kskcl='" + kskcl + '\'' + ", yswf='" + yswf + '\'' + ", rksj='" + rksj + '\'' + ", cksj='" + cksj + '\'' + ", ztcpyqddq='" + ztcpyqddq + '\'' + ", kccz='" + kccz + '\'' + '}'; } }
3、java读取csv文件
package zgw.com; //统计 import com.opencsv.CSVWriter; import zgw.pojo.Domain; import java.io.*; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class CSV { public static void main(String[] args) { //读取csv的数据 ArrayList<Domain> domains = readCSV(); System.out.println(domains); } private static ArrayList<Domain> readCSV() { ArrayList<Domain> list=new ArrayList<>(); InputStreamReader inputStreamReader; try { inputStreamReader = new InputStreamReader(new FileInputStream("C:\\Users\\wyq\\Desktop\\test1.csv")); Scanner inputStream = new Scanner(inputStreamReader); inputStream.nextLine(); // 第一行表头不要 while (inputStream.hasNext()) { String data = inputStream.nextLine().replace(" ",""); // 得到一行数据 String[] line = data.split(","); // 分割数据 if (line.length>0){ Domain domain=new Domain(); domain.setScqy(line[0]); domain.setCpdl(line[1]); domain.setCpxl(line[2]); domain.setCpph(line[3]); domain.setXqjhsl(line[4]); domain.setQdck(line[5]); domain.setDccf(line[6]); domain.setCkmax(line[7]); domain.setJhsj(line[8]); domain.setDrpcbh(line[9]); domain.setCkl(line[10]); domain.setRkl(line[11]); domain.setQckc(line[12]); domain.setZtkc(line[13]); domain.setKskcl(line[14]); domain.setYswf(line[15]); domain.setRksj(line[16]); domain.setCksj(line[17]); if (line.length > 18) { domain.setZtcpyqddq(line[18]); } list.add(domain); } } inputStream.close(); System.out.println("数据读取成功"); return list; } catch (IOException e) { e.printStackTrace(); return null; } } }
4、java将list集合数据写入csv文件
package zgw.com; //统计 import com.opencsv.CSVWriter; import zgw.pojo.Domain; import java.io.*; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class CSV { public static void main(String[] args) { //读取csv的数据 ArrayList<Domain> domains = readCSV(); System.out.println(domains); //写入csv数据 writeCSV(domains); } private static void writeCSV(ArrayList<Domain> list) { //设置路径及文件名称 String fileName = "C:\\Users\\wyq\\Desktop\\test1.csv"; CSVWriter writer = null; //装业务数据集合 List<String[]> data = new ArrayList<>(); String[] header={"生产企业","产品大类","产品小类","产品牌号","需求计划数量","起点仓库","吨仓储费(元/吨)","仓库容量最大值","计划时间","导入批次编号","出库量","入库量","期初库存","在途库存","可售库存量","已售未发","入库时间","出库时间","在途产品预计到达期"}; data.add(header); try { for (Domain domain : list) { String[] string = domain.toString().replace("'","").replace("}","").split(","); String[] string1 =new String[19]; for (int i = 0; i < string.length; i++) { if (string[i]!=null){ if (string[i].split("=")[1].equals("null")){ string1[i]=""; }else { string1[i]=string[i].split("=")[1]; } }else{ string1[i]=""; } } data.add(string1); } // 创建文件所在目录 FileOutputStream fileOutputStream = new FileOutputStream(fileName); fileOutputStream.write(0xef); //加上这句话 fileOutputStream.write(0xbb); //加上这句话 fileOutputStream.write(0xbf); //加上这句话 writer = new CSVWriter(new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8.name()), CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.DEFAULT_ESCAPE_CHARACTER, CSVWriter.DEFAULT_LINE_END); writer.writeAll(data); System.out.println("数据写入成功"); } catch (Exception e) { System.out.println("将数据写入CSV出错:"+e); } finally { if (null != writer) { try { writer.flush(); writer.close(); } catch (IOException e) { System.out.println("关闭文件输出流出错:"+e); } } } } private static ArrayList<Domain> readCSV() { ArrayList<Domain> list=new ArrayList<>(); InputStreamReader inputStreamReader; try { inputStreamReader = new InputStreamReader(new FileInputStream("C:\\Users\\wyq\\Desktop\\test1.csv")); Scanner inputStream = new Scanner(inputStreamReader); inputStream.nextLine(); // 第一行表头不要 while (inputStream.hasNext()) { String data = inputStream.nextLine().replace(" ",""); // 得到一行数据 String[] line = data.split(","); // 分割数据 if (line.length>0){ Domain domain=new Domain(); domain.setScqy(line[0]); domain.setCpdl(line[1]); domain.setCpxl(line[2]); domain.setCpph(line[3]); domain.setXqjhsl(line[4]); domain.setQdck(line[5]); domain.setDccf(line[6]); domain.setCkmax(line[7]); domain.setJhsj(line[8]); domain.setDrpcbh(line[9]); domain.setCkl(line[10]); domain.setRkl(line[11]); domain.setQckc(line[12]); domain.setZtkc(line[13]); domain.setKskcl(line[14]); domain.setYswf(line[15]); domain.setRksj(line[16]); domain.setCksj(line[17]); if (line.length > 18) { domain.setZtcpyqddq(line[18]); } list.add(domain); } } inputStream.close(); System.out.println("数据读取成功"); return list; } catch (IOException e) { e.printStackTrace(); return null; } } }