public class readProperties {
private static Properties pro = new Properties();
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
public static void main(String[] args) throws IOException {
System.out.println(getPropertiesFile("src/count.properties", "num"));
}
public static String getPropertiesFile(String filepath, String key)
throws IOException {
InputStream input = new FileInputStream(new File(filepath));
pro.load(input);
String source = pro.getProperty(key);
if (source == null) {
source = "g" + sdf.format(new Date()) + "_00001";
pro.setProperty(key, source);
} else {
pro.setProperty(key, getNumber(source));
}
try {
OutputStream os = new FileOutputStream(new File(filepath));
pro.store(os, "pk identify");
pro.clear();
os.flush();
return getNumber(source);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static String getNumber(String source) {
String str = source.substring(source.lastIndexOf("_") + 1);
int num = -1;
try {
num = Integer.parseInt(str) + 1;
} catch (NumberFormatException e) {
return null;
}
String r = "0000" + num;
r.substring(r.length() - 5);
return "g" + sdf.format(new Date()) + "_" + r;
}
}
private static Properties pro = new Properties();
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
public static void main(String[] args) throws IOException {
System.out.println(getPropertiesFile("src/count.properties", "num"));
}
public static String getPropertiesFile(String filepath, String key)
throws IOException {
InputStream input = new FileInputStream(new File(filepath));
pro.load(input);
String source = pro.getProperty(key);
if (source == null) {
source = "g" + sdf.format(new Date()) + "_00001";
pro.setProperty(key, source);
} else {
pro.setProperty(key, getNumber(source));
}
try {
OutputStream os = new FileOutputStream(new File(filepath));
pro.store(os, "pk identify");
pro.clear();
os.flush();
return getNumber(source);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static String getNumber(String source) {
String str = source.substring(source.lastIndexOf("_") + 1);
int num = -1;
try {
num = Integer.parseInt(str) + 1;
} catch (NumberFormatException e) {
return null;
}
String r = "0000" + num;
r.substring(r.length() - 5);
return "g" + sdf.format(new Date()) + "_" + r;
}
}