我正在放置文件" Seq.properties "在SAP服务器位置。
文件 Seq.properties 包含2个变量的计数。每次调用java函数时,它都会增加2个变量的数量,并将该文件存储在SAP服务器位置。
守则如下,
public final static String executeRegNo() {
File file=new File("C:/Users/xyz/Desktop/Files/Seq.properties");
Properties properties=new Properties();
//1. If file Seq.properties doesnt exist, it first initializes TransNum to 0 and RegId to 1
if (!file.exists()) {
try
{
file.createNewFile();
properties.setProperty("TransNum", "0");
properties.setProperty("RegId", "1");
properties.store(new FileOutputStream(file), null);
}
catch (IOException e)
{
e.printStackTrace();
}
}
//2. If file Seq.properties exist, it reads the file Seq.properties and
//it increments TransNum to prev_value+1 and RegId remains the same 1
try
{
properties.load(new FileInputStream(file));
}
catch (IOException e)
{
e.printStackTrace();
}
String transId = properties.getProperty("TransNum");
String RegisId = properties.getProperty("RegId");
properties.setProperty("TransNum", String.valueOf(Integer.parseInt(transId) + 1));
properties.setProperty("RegId", String.valueOf(Integer.parseInt(RegisId)));
//3. the incremented value is stored in the Seq.properties file
try
{
properties.store(new FileOutputStream(file), null);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
String RId = properties.getProperty("RegId");
String TId = properties.getProperty("TransNum");
DecimalFormat df = new DecimalFormat("00");
String R = String.valueOf(df.format(Integer.parseInt(RId)));
DecimalFormat df1 = new DecimalFormat("0000");
String T = String.valueOf(df1.format(Integer.parseInt(TId)));
return R + T;
}
假设由于系统故障,如果文件 Seq.properties 被删除,如何替换该文件的内容?
请提供宝贵的意见,因为这是至关重要的要求。
提前致谢
答案 0 :(得分:1)
我的建议是将变量的内容存储在数据库中,每当您检索 Seq.properties 文件时,加载数据库中的值。 如果该文件不存在,则会创建具有先前值
的新文件