背景:我们在进行性能测试时,需要构造测试数据,即参数化文件,如下:
上面的文件内容,我们可以通过Java代码轻松实现,主要代码解释:
All 代码(其实我也看不懂,但是会改就行啦)
package file;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class FileWrite {
public static void main(String[] args) {
String filepath= "C:\Users\lixiaojuan\Desktop\lr\末班\test2.txt";
checkFile(filepath);
writeContent("name,pwd,salt
",filepath,true);
for(int i=1;i<=50;i++){
System.out.println("add ---------"+i);
writeContent("test"+i+"@bbs-step.com,123456,e10adc39749ba59abbe56e057f20f883e"+"
",filepath,true);
}
}
/**
* 写入内容到文件
*
* @param number
* @param filename
* @return
*/
public static boolean writeContent(String c, String path, boolean isAppend) {
File f = new File(path);
try {
FileOutputStream fos = new FileOutputStream(path, isAppend);
OutputStreamWriter writer = new OutputStreamWriter(fos, "UTF-8");
writer.write(c);
writer.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public static void checkFile(String filepath) {
File f = new File(filepath);
if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
return;
} else {
f.delete();
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}