Write data appended to the end of each line in the CSV file

Write data appended to the end of each line in the CSV file

1. Demand analysis

When using the jmeter tool for concurrent testing, we sometimes need to append the data extracted from the json extractor to the end of each line of the csv file for later use in the program. eg: When the program executes the first thread, it appends the extracted new data to the end of the first line, and when the program executes the second thread, it appends the extracted new data to the end of the second line, and so on ...

2. Data preparation

(1) Three csv files need to be prepared, namely read.csv, read-copy.csv, write.csv. Remember to set to UTF-8 format.
csv file data preparation
Explain:
read.csv is provided to the jmeter tool itself for reading, as shown below:
read.csv
read_copy.csv and write.csv are used by users to write and read java programs, and the code is placed in the BeanShell post processor
read_copy.csv和write.csv

Three. First complete the writing and debugging of the code in eclipse

The implementation of the code:
(1) The first run of the program: the second line of data in read_copy.csv (removing the header field of the first line) and the new data obtained (the extracted new data songteng2012) are appended to the write.csv file In the second line (the header field of the first line has been added during the data preparation stage).
(2) Then delete the second line of data in read_copy.csv.
(3) The second run of the program: the second line of data in read_copy.csv (remove the header field of the first line) and the acquired new data (extracted new data songteng2012) are added to the third line of the write.csv file .
(4) Then delete the second line of data in read_copy.csv.
So on and so forth…

import java.io.File;

import java.io.BufferedReader;
import java.io.BufferedWriter;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;



public class Opera_csv{
	
	//eclipse编写中我们只需要read_copy.csv和write.csv这2个文件即可.		
	String readPath = "D:\\Jmeter\\apache-jmeter-5.1.1\\bin\\生成密钥实战Opera_csv\\read_copy.csv";
	String writePath = "D:\\Jmeter\\apache-jmeter-5.1.1\\bin\\生成密钥实战Opera_csv\\write.csv";
	
	
		
	//读取read_copy中行的数据和提取到的新数据,把这些数据追加写到write.csv中
	public void write_csv() throws IOException {	
				
		BufferedReader r = new BufferedReader(new FileReader(new File(readPath)));			
		BufferedWriter w =new BufferedWriter(new FileWriter(writePath,true));						
		String temp = null;
		r.readLine();
		if((temp = r.readLine()) != null){
			temp =temp + "," + "提取的新数据songteng2012";
			w.write(temp);
			w.write("\r\n");			
		}			
			
		w.close();
		r.close();
		
		
		del_row();
																			
	}
		
		
	//删除read_copy中的第二行数据
	public void del_row() throws IOException{
				
		
		BufferedReader r = new BufferedReader(new FileReader(new File(readPath)));
		StringBuffer str = new StringBuffer(4096);			
		String temp = null;			
		int line = 0;
		int lineDel = 2;			
		while((temp=r.readLine()) != null) {
			line++;
			if(line == lineDel) continue;
			str.append(temp).append("\r\n");
		}
		r.close();			
		BufferedWriter w =new BufferedWriter(new FileWriter(readPath));
		w.write(str.toString());
		w.close();									
		
	}
	
	
	public static void main(String[] args) throws IOException
	{	

		Opera_csv s = new Opera_csv();
		s.write_csv();
						
	
	}
	

}

Four. Program execution

1. Data before execution
Pre-execution data
2. Data after program execution once
Data after execution

Five. The program is placed in the BeanShell post-processing program of jmeter

It should be noted in the BeanShell post-processor that we do not need to declare classes.
BeanShell post processor

Published 21 original articles · Like1 · Visits 380

Guess you like

Origin blog.csdn.net/songteng2012/article/details/105432284