
String filename = "D:/桌面/token.txt"//定义一个变量用查询token文件
File file = new File(filename)//定义File文件为刚才定义的变量
//进行判断 如果 这个文件存在就删除这个文件
if(file.exists()){
file.delete()
}
FileWriter fstream = new FileWriter ("文件目录",true)//找到文件的
BufferedWriter out =new BufferedWriter (fstream )//设置好需要写入的文件
out.writer(${变量})//写入的内容
out.writer(",")//用都好分割
out.writer(${变量})//写入的内筒
out.writer(System,.getProperty("line.separtor"))
out.close()
fstream.close()/关闭整个文件
file:要写入数据的 File 对象。
append:如果 append 参数为 true,则将字节写入文件末尾处,相当于追加信息。如果 append 参数为 false, 则写入文件开始处。构造与某个文件描述符相关联的 FileWriter 对象。
1、public void write(int c) throws IOException 写入单个字符c。
2、public void write(char [] c, int offset, int len) 写入字符数组中开始为offset长度为len的某一部分。
3、public void write(String s, int offset, int len) 写入字符串中开始为offset长度为len的某一部分。
遇到的问题:
1.打印出来的中文乱码
解决方案:
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=0i<leni++){
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()
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)