Search code examples
jmeterbeanshell

accessing file from jmeter beanshell sample using filepath


I'm trying to access one file from beanshell sampler. However it shows me error as below :

Encounter "(C:"at line 9, column 2)

file path name is C:/Test/test.txt. It seems i can't use C:/ drive in beanshell sampler? please let me know how can I resolve this


Solution

  • Most likely you have a syntax error in your Beanshell script, try using the below reference code which is reading the file and prints its contents into jmeter.log

    String test = org.apache.commons.io.FileUtils.readFileToString(new File("C:/Test/test.txt"));
    log.info("test.txt file content is: " + test);
    

    Demo:

    JMeter Beanshell Read From File

    See How to Use BeanShell: JMeter's Favorite Built-in Component article to learn more about Beanshell scripting in JMeter.


    NB: it may be faster and easier to use __FileToString function like:

    ${__FileToString(c:\test\test.txt,,)}
    

    JMeter FileToString Function