import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.Writer;
import java.util.Random;
import java.util.Arrays;
FileWriter fstream = new FileWriter("D:\\Jmeter\\测试集_rita\\添加产品.csv",false);
// 1. 设置为true时,从第2行开始插入数据;设置为false时,从第一行开始插入数据。
// 2. 设置为true时,后面运行时不会覆盖原先的数据;设置为false时,会覆盖原先的数据。
BufferedWriter out = new BufferedWriter(fstream);
Object result=vars.getObject("pro_res");
log.info("结果是"+ result);
int len = result.size();
log.info("个数是"+ len);
String p_ids="";
String p_types="";
String p_grades="";
String p_commerce_types="";
String p_air_conditioning_types="";
for(int i=0;i String p_id= result.get(i).get("product_id").toString(); String p_type=result.get(i).get("product_types").toString(); String p_type2 = p_type.replace(","," "); //写入文件时是以“,”区分单元格的,所以得替换原先的符号 String[] p_type3 = p_type2.split(" "); String p_grade= result.get(i).get("grades").toString(); String p_grade2=p_grade.replace(","," "); String[] p_grade3 = p_grade2.split(" "); String p_commerce_type= result.get(i).get("commerce_types").toString(); String p_commerce_type2=p_commerce_type.replace(","," "); String[] p_commerce_type3 = p_commerce_type2.split(" "); String p_air_conditioning_type=result.get(i).get("air_conditioning_types").toString(); String p_air_conditioning_type2=p_air_conditioning_type.replace(","," "); String[] p_air_conditioning_type3 = p_air_conditioning_type2.split(" "); log.info("p_ids为" + p_id); log.info("p_types为" + p_type2); log.info("p_grades为" + p_grade2); log.info("p_commerce_types为" + p_commerce_type2); log.info("p_air_conditioning_types为" + p_air_conditioning_type2); log.info("长度为" + p_type3.length); log.info("长度为" + p_grade3.length); log.info("长度为" + p_commerce_type3.length); log.info("长度为" + p_air_conditioning_type3.length); Random random = new Random(); int i = random.nextInt(p_type3.length); int j = random.nextInt(p_grade3.length); int k = random.nextInt(p_commerce_type3.length); int l = random.nextInt(p_air_conditioning_type3.length); out.write(p_id + ","); if (p_type3.length == 1){ out.write(null + ","); //当属性为空时,需要传null }else{ out.write(p_type3[i].toString() + ","); } if (p_grade3.length == 1){ out.write(null + ","); }else{ out.write(p_grade3[j].toString() + ","); } if (p_commerce_type3.length == 1){ out.write(null + ","); }else{ out.write(p_commerce_type3[k].toString() + ","); } if (p_air_conditioning_type3.length == 1){ out.write(null + ","); }else{ out.write(p_air_conditioning_type3[l].toString() + ","); } } out.close(); fstream.close();