将 UTF-8 内容写入文件的 Beanshell 脚本

Beanshell script to write UTF-8 content to file

我正在使用下面的 Beanshell 脚本将提取的名字从 API 响应写入文件。 FirstName 是波兰语,当我们写入文件时,它会转换为一些特殊字符。

我们如何将准确的值写入文件?如果有人可以提供帮助,我们将不胜感激。

File file = new File("userinfo.csv");
FileWriter fstream = new FileWriter(file, true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(vars.get("FirstName")
  1. Since JMeter 3.1 you're supposed to be using JSR223 Test Elements and Groovy language for scripting 所以考虑迁移,相同的代码无需更改就可以工作
  2. 考虑在完成写入后调用flush() function on the stream and closing它,否则数据将会丢失
  3. 确保设置 file.encoding property to UTF-8 否则您的默认语言环境可能不适合显示波兰国家字符

演示:

另请注意,如果您 运行 您的脚本具有 > 1 个线程,您将面临 race condition resulting in data corruption or loss so it worth considering switching to i.e. Flexible File Writer 或确保您的代码 运行 一次仅具有 1 个线程.