标签:his main 文件名 hmm [] this i++ write app
public static void main(String[] args) throws IOException {
List<Object[]> rows = new ArrayList<Object[]>();
String filePath = "D:\\开发计划\\test\\";
String fileName = "dataSoure";
for(int i=0;i<10000;i++){
Random rd = new Random();
int in = rd.nextInt()*10000+1;
String out = String.valueOf(in).substring(1, 6);
String plateNo = "浙A"+out;
String vehicleFrameNo = "LJ12EKR25F4R"+out;
String engineNo = "LJ12EK"+out;
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String payFlowId = "A"+format.format(new Date())+out;
String[] array = {plateNo,",",vehicleFrameNo,",",engineNo,",",payFlowId};
rows.add(array);
}
boolean a = createTxtFile(rows,filePath,fileName);
System.out.println(a);
}
/**
* 生成.TXT格式文件,行数几乎无上限
*/
public static boolean createTxtFile(List<Object[]> rows, String filePath, String fileName) {
// 标记文件生成是否成功
boolean flag = true;
try {
// 含文件名的全路径
String fullPath = filePath + File.separator + fileName + ".txt";
File file = new File(fullPath);
if (file.exists()) { // 如果已存在,删除旧文件
file.delete();
}
file = new File(fullPath);
file.createNewFile();
// 遍历输出每行
PrintWriter pfp = new PrintWriter(file);
for (Object[] rowData : rows) {
StringBuffer thisLine = new StringBuffer("");
for (int i = 0; i < rowData.length; i++) {
Object obj = rowData[i]; // 当前字段
// 格式化数据
String field = "";
if (null != obj) {
if (obj.getClass() == String.class) { // 如果是字符串
field = (String) obj;
}
} else {
field = " "; // null时给一个空格占位
}
// 拼接所有字段为一行数据,用tab键分隔
if (i < rowData.length - 1) { // 不是最后一个元素
thisLine.append(field);
} else { // 是最后一个元素
thisLine.append(field);
}
}
pfp.print(thisLine.toString() + "\n");
}
pfp.close();
} catch (Exception e) {
flag = false;
e.printStackTrace();
}
return flag;
}
标签:his main 文件名 hmm [] this i++ write app
原文地址:http://www.cnblogs.com/liuyitan/p/6871804.html