0

I am using below Beanshell script to write extracted firstname from API response to File. FirstName is in polish language, when we write to file it get converted to some special characters.

How can we write exact value to a file ? Appreciate if any one can help on this.

File file = new File("userinfo.csv");
FileWriter fstream = new FileWriter(file, true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(vars.get("FirstName")
Kumar
  • 399
  • 4
  • 16

1 Answers1

0
  1. Since JMeter 3.1 you're supposed to be using JSR223 Test Elements and Groovy language for scripting so consider migrating, the same code should work without changes
  2. Consider calling flush() function on the stream and closing it once you done writing otherwise the data will be lost
  3. Make sure to set file.encoding property to UTF-8 otherwise your default locale might not be suitable for displaying Polish national characters

Demo:

enter image description here

Also be aware that if you run your script with > 1 thread you will face a race condition resulting in data corruption or loss so it worth considering switching to i.e. Flexible File Writer or ensure that your code is being run with 1 thread only at a time.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Thanks Dmitri, i have used System.setProperty("file.encoding", "UTF-8"); in beanshell script but still it is writing special charectors. Is it possible to set File.Encoding at FileWriter fstream = new FileWriter(file, true); BufferedWriter out = new BufferedWriter(fstream); which can solve the issue – Kumar Mar 17 '22 at 08:01
  • It is, something like `FileOutputStream fos = new FileOutputStream(file); OutputStreamWriter writer = new OutputStreamWriter(fos, StandardCharsets.UTF_8); BufferedWriter out = new BufferedWriter(writer);`. I also think you need to set the property in [*system.properties* file](https://www.blazemeter.com/blog/apache-jmeter-properties-customization/) (lives in "bin" folder of your JMeter installation. And why are you still using Beanshell? – Dmitri T Mar 17 '22 at 11:53