jmeter-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From apurv30 <>
Subject Re: Setting CSV file name as a variable
Date Tue, 15 Mar 2011 06:22:45 GMT
Hello Dan,

I have written a program that can read from a text file with values written

The code parses the line, splits the strings and stores the value in a
variable  and parses it into an interger..

It also has the regex pattern required to do a search replacement. I did it
for an xml file including quotes.
If you want .csv file you can check source forge for code that reads from
file, and to replace u can use below code.

The sample code is copy pasted below..

import java.util.*;
import java.util.regex.*;

****** to read code from file *******
String target = "";
String path = "values.txt";
String[] s = null;

try {
            File file = new File(path);
            BufferedReader br = new BufferedReader(
                                new InputStreamReader(
                                new FileInputStream(file)));                 

            br.mark(200); // specify a large enough = total no of characters
in file
            target = "no_of_users";
            while((line = br.readLine()) != null){           
                    if(line.indexOf(target) != -1){
                s = line.split("\\=");           
                no_of_users = s[1];   

} catch(IOException e){
                    System.out.println("Error: Unable to Load Values from
file check Values.txt " + e.getMessage());

******* for text replacement in a file  including pattern matching

            File file = new File(test_file);           
            // Read the entire contents of jmeter file
            BufferedReader reader = new BufferedReader( new FileReader
            line  = null;
            StringBuilder stringBuilder = new StringBuilder();
            String ls = System.getProperty("line.separator");
            while( ( line = reader.readLine() ) != null ) {
                stringBuilder.append( line );
                stringBuilder.append( ls );

// Replace Number of Threads
            Pattern pattern =
            Matcher matcher = pattern.matcher(stringBuilder);
            String output =
matcher.replaceAll("ThreadGroup.num_threads\\\">" + no_of_users + "");

the code replace the value in the string 
with the value specified in the variable - no_of_users
for random text u can specify [a-z]*[A-Z]*[0-9]*

BufferedWriter out = new BufferedWriter(new FileWriter(test_file));
        catch(Exception e)
            System.out.println("Error : " + e.getMessage());

Apoorv Kodera

View this message in context:
Sent from the JMeter - User mailing list archive at

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message