java 文件生成_java文件生成

packagecom.gcy.test.util;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.IOException;importjava.util.Random;importorg.apache.commons.io.output.FileWriterWithEncoding;importorg.junit.Test;public classTestGroupGenerator {private String TARGET_FILENAME = "group_data";private String TARGET_PATH = "/Users/test/" +TARGET_FILENAME;

Integer[] groupIds= {16,18,19,20,26,27};

String[] name= {"三坊七巷一店","温泉一店","温泉二店","新港一店","温泉三店","三坊七巷二店"};

Integer[] deviceCounts= {12,1,1,6,1,5};

Integer[] onlineCounts= {3,0,4,0,1,1};

@Testpublic voidgenerateFile() {//如果已经存在相同文件则删除

File targetFile = newFile(TARGET_PATH);

targetFile.delete();try{

writtenFile(targetFile);

}catch(IOException e) {

e.printStackTrace();

}

}//写文件

void writtenFile(File file) throwsIOException {//写文件

file.createNewFile();

FileWriterWithEncoding fw= new FileWriterWithEncoding(file, "utf-8", true);

BufferedWriter bw=newBufferedWriter(fw);try{for(int i = 0; i < deviceCounts.length; i ++) {

String onString= "'" + groupIds[i] + "'" + ":{deviceCount:" + deviceCounts[i] + "," +

"onlineCount:" + onlineCounts[0] + "," +

"staCount:" + getInt(100) + "," +

"upFlow:" + getInt(10000) + "," +

"downFlow:" + getInt(10000) + "," +

"gateway:" + getIp() + "," +

"dns:" + getIp() + "," +

"name:" + getString(name[i]) +

"}";

bw.write(onString+"\r\n");

}

bw.flush();

fw.flush();

System.out.println("done");

}catch(FileNotFoundException e) {

e.printStackTrace();

}finally{

bw.close();

fw.close();

}

}privateString getString(String ob) {return "'" + ob + "'";

}private int getInt(inti) {

Random rand= newRandom();returnrand.nextInt(i);

}privateString getIp() {

String ip= getInt(255) + "." + getInt(255) + "." + getInt(255) + "." + getInt(255);returngetString(ip);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值