由于经常要拉取数据库记录生成csv,所以封装了两个工具。
可以根据需求对这两个工具进行相应的改写。
一种是接受两个参数,一个是csv生成的路径,这个路径分windows和linux,windows系统要注意文件分隔符为\\而linux文件分隔符为//,比如windows下为
E:\\csv\\而Linux下为//www//zip//,但是注意这个传入的路径是包含文件名称的,当然你也可以逻辑上进行改写,举例传入路径 E:\\csv\\good\\test.csv
这样会在E盘csv文件夹下的good文件夹下生成一个test.csv。
第二个参数是实体类链表,因此这个工具的作用是读取你传入的实体类链表并生成一个csv文件到你传入的路径。
import com.nonagon.clinicaltest.entity.ClinicalConditon; import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.util.List; /** * @ClassName: * @author: flitsneak nonagon * @date: 2020/12/23 */ public class CSVReader { //传入两个参数,一个是cpath 即生成路径,一个是实体类链表 public static Boolean GenerateCSV(String cpath, List<ClinicalConditon> list){ BufferedWriter bufferedWriter=null; FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(cpath,true); bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream)); bufferedWriter.write("clinicalconditionid"); bufferedWriter.write(","); bufferedWriter.write("ad0"); bufferedWriter.write(","); bufferedWriter.write("data0"); bufferedWriter.write(","); bufferedWriter.write("ad1"); bufferedWriter.write(","); bufferedWriter.write("data1"); bufferedWriter.write(","); bufferedWriter.write("ad2"); bufferedWriter.write(","); bufferedWriter.write("data2"); bufferedWriter.write(","); bufferedWriter.write("speciesType"); bufferedWriter.write(","); bufferedWriter.write("breedType"); bufferedWriter.write(","); bufferedWriter.write("vitalType"); bufferedWriter.write(","); bufferedWriter.write("gender"); bufferedWriter.write(","); bufferedWriter.write("age"); bufferedWriter.write(","); bufferedWriter.write("weight"); bufferedWriter.write(","); bufferedWriter.write("petImageUrl"); bufferedWriter.newLine(); for(int i = 0; i < list.size(); i++ ) { for (int j = 0; j < list.get(i).getClinicalDataList().size(); j++) { bufferedWriter.write(String.valueOf(list.get(i).getClinicalConditonId())); bufferedWriter.write(","); bufferedWriter.write("0"); bufferedWriter.write(","); bufferedWriter.write(String.valueOf(list.get(i).getClinicalDataList().get(j).getData0())); bufferedWriter.write(","); bufferedWriter.write("0"); bufferedWriter.write(","); bufferedWriter.write(String.valueOf(list.get(i).getClinicalDataList().get(j).getData1())); bufferedWriter.write(","); bufferedWriter.write("0"); bufferedWriter.write(","); bufferedWriter.write("25.04"); bufferedWriter.write(","); bufferedWriter.newLine(); } } bufferedWriter.close(); } catch (Exception e) { e.printStackTrace(); return false; } return true; } } |
第二个工具是对第一个工具的改写,传入的是单个实体而不是实体类链表。
import com.nonagon.clinicaltest.entity.ClinicalConditon; import com.nonagon.clinicaltest.entity.ClinicalData; import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.List; /** * @ClassName: * @author: flitsneak nonagon * @date: 2020/12/23 */ public class CSVReader2 { //传入两个参数,一个是cpath 即生成路径,一个是实体类 public static Boolean GenerateCSV(String cpath, ClinicalConditon clinicalCondition){ BufferedWriter bufferedWriter=null; FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(cpath,true); bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream,"utf-8")); bufferedWriter.write("ad0"); bufferedWriter.write(","); bufferedWriter.write("data0"); bufferedWriter.write(","); bufferedWriter.write("ad1"); bufferedWriter.write(","); bufferedWriter.write("data1"); bufferedWriter.write(","); bufferedWriter.write("ad2"); bufferedWriter.write(","); bufferedWriter.write("data2"); bufferedWriter.write(","); bufferedWriter.write("name"); bufferedWriter.write(","); bufferedWriter.write("birthday"); bufferedWriter.write(","); bufferedWriter.write("weight"); bufferedWriter.write(","); bufferedWriter.write("gender"); bufferedWriter.write(","); bufferedWriter.write("isNeutered"); bufferedWriter.write(","); bufferedWriter.write("notes"); bufferedWriter.write(","); bufferedWriter.write("selectMeasuringPart"); bufferedWriter.newLine(); bufferedWriter.write("0"); bufferedWriter.write(","); bufferedWriter.write("25"); bufferedWriter.write(","); bufferedWriter.write("0"); bufferedWriter.write(","); bufferedWriter.write("25"); bufferedWriter.write(","); bufferedWriter.write("0"); bufferedWriter.write(","); bufferedWriter.write("25"); bufferedWriter.write(","); bufferedWriter.write("null"); bufferedWriter.write(","); bufferedWriter.write("Invalid date"); bufferedWriter.write(","); bufferedWriter.write(clinicalCondition.getWeight().toString()); bufferedWriter.write(","); bufferedWriter.write(clinicalCondition.getGender()); bufferedWriter.write(","); bufferedWriter.write("false"); bufferedWriter.write(","); bufferedWriter.write("null"); bufferedWriter.write(","); bufferedWriter.write(clinicalCondition.getVitalType()); bufferedWriter.newLine(); for (int i = 0; i < clinicalCondition.getClinicalDataList().size(); i++) { bufferedWriter.write(String.valueOf(clinicalCondition.getClinicalDataList().get(i).getAd0())); bufferedWriter.write(","); bufferedWriter.write(String.valueOf(clinicalCondition.getClinicalDataList().get(i).getData0())); bufferedWriter.write(","); bufferedWriter.write(String.valueOf(clinicalCondition.getClinicalDataList().get(i).getAd1())); bufferedWriter.write(","); bufferedWriter.write(String.valueOf(clinicalCondition.getClinicalDataList().get(i).getData1())); bufferedWriter.write(","); bufferedWriter.write("0"); bufferedWriter.write(","); bufferedWriter.write("25.04"); bufferedWriter.write(","); bufferedWriter.newLine(); } bufferedWriter.close(); } catch (Exception e) { e.printStackTrace(); return false; } return true; } public static void main(String[] args) { ClinicalConditon clinicalConditon = new ClinicalConditon(); clinicalConditon.setClinicalConditonId(134); clinicalConditon.setSpeciesType("ll"); clinicalConditon.setBreedType("k"); clinicalConditon.setVitalType("c"); clinicalConditon.setGender("m"); clinicalConditon.setAge(12); clinicalConditon.setWeight(12.3); clinicalConditon.setPetImageUrl("eee"); ClinicalData clinicalData = new ClinicalData(); clinicalData.setAd0(1); clinicalData.setData0(33.6); clinicalData.setAd1(2); clinicalData.setData1(35.6); List<ClinicalData> list = new ArrayList<>(); list.add(clinicalData); clinicalConditon.setClinicalDataList(list); String path = "e://csv//test.csv"; CSVReader2 csvReader2 = new CSVReader2(); csvReader2.GenerateCSV(path,clinicalConditon); } } |
其实hutool工具包的生成csv的方式也很简单,但是要传入String数组,代码量上并没有精简,所以就这样用吧。
标签:String,生成,write,bufferedWriter,import,工具,csv,clinicalConditon From: https://blog.csdn.net/nonagontech/article/details/143751455