Jmeter的beanshell中,向txt写入字符串数据的几种方式。
1.字符流
//指定输出文件所在的目录和名字,
String filename = "D://test.txt";//此处可以用txt,也可以用csv
//true为追加写入内容,如果每次都清空重写,将true删掉
FileWriter writer = new FileWriter(filename,true);
String data_name1 = vars.get("name1");//name1是我本地正则表达式提取出来的变量
String data_name2 = vars.get("name2");
writer.write(data_name1+","+data_name2);//多条数据用逗号分割
writer.write("\r\n");//换行操作,用于下一组数据换行展示
writer.close();
2.字节流
String filename = "F://review.txt";
File file = new File(filename);
FileOutputStream fs = new FileOutputStream(file,true);
fs.write("asdada".getBytes());
fs.write("\r\n".getBytes());
fs.close();
3.封装成类
public class JmeterReadAndWrite {
public static void writeTxt(String txtPath, String content){
File file = new File(txtPath);
FileOutputStream fs = new FileOutputStream(file,true);
fs.write(content.getBytes());
fs.write("\r\n".getBytes());
// fs.flush();
fs.close();
}
}
String str = vars.get("review_id");//正确的写法
String txtPath = "F:\\review.txt";
JmeterReadAndWrite.writeTxt(txtPath,str);
原文链接:https://blog.csdn.net/qq_34365469/article/details/101419994