从.properties读写数据,生成唯一数字,然后模拟用户信息

private Staff generateStaff() {
// TODO Auto-generated method stub
int unique = generateUniqueValue();
String location = generateLocation();
String unit = generateUnit();
// Staff staff = new Staff("YB"+unique, "张三"+unique, false, "LJZ", "VIII",
// "muni");
Staff staff = new Staff("YB" + unique, "张三" + unique, location, unit, "team" + unique, false);
return staff;
}

private String generateUnit() {
// TODO Auto-generated method stub
String[] units = { "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X" };
int index = (int) (Math.random() * units.length);
return units[index];
}

private String generateLocation() {
// TODO Auto-generated method stub
String[] locations = { "DL", "ZJ", "LJZ" };
int index = (int) (Math.random() * locations.length);

return locations[index];
}

private int generateUniqueValue() {
FileInputStream fis = null;
FileOutputStream fos = null;
// 创建计数器
int count = 0;
try {
// 创建Properties对象
Properties prop = new Properties();
// 将文件封装在File对象中
File file = new File("classpath:unique.properties");
// 判断文件是否存在,如果不存在就创建
if (!file.exists()) {
file.createNewFile();
}
// 将文件对象放入流对象中
fis = new FileInputStream(file);
// 加载流文件
prop.load(fis);
// 从配置文件中获取unique的值(次数)
String unique = prop.getProperty("unique");
// 判断times是否为空,如果有值存入count中
if (unique != null) {
count = Integer.parseInt(unique);
}
// 每访问一次count变加1
count++;
// 将新的count写入prop中
prop.setProperty("unique", count + "");
// 创建输出流
fos = new FileOutputStream(file);
// 将配置信息重新写入文件中,并加上注释信息comments(也可不写)
prop.store(fos, "unique value to make the string unique!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null)
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

return count;

}

/*
* public long getTime() 返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
* public void setTime(long time) 设置此 Date 对象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后
* time 毫秒的时间点。
*
* 实例: 求出sourceTime,过minuteNumber分钟后的时间 public static String getPreTime(String
* sourceTime, String minuteNumber) { SimpleDateFormat format = new
* SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //SimpleDateFormat format = new
* SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); String mydate1 = ""; try { Date
* date1 = format.parse(sourceTime); long Time = (date1.getTime() / 1000) +
* Integer.parseInt(minuteNumber) * 60; date1.setTime(Time * 1000); mydate1 =
* format.format(date1); } catch (Exception e) { } return mydate1; }
*/

posted @ 2018-06-19 10:33  董永辉Bruno  阅读(376)  评论(0)    收藏  举报