Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果服务器中的文件被删除,如何替换文件内容?_Java_File - Fatal编程技术网

Java 如果服务器中的文件被删除,如何替换文件内容?

Java 如果服务器中的文件被删除,如何替换文件内容?,java,file,Java,File,我正在将文件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 P

我正在将文件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被删除,如何替换该文件的内容

请提供您的宝贵意见,因为这是关键要求

提前感谢

我的建议是将变量的内容存储在数据库中,每当您检索Seq.properties文件时,从数据库中加载该值。
如果文件不存在,它将使用以前的值创建新文件

请发布一些代码。@AndyTurner-代码通过替换该文件的内容来发布。您的意思是要恢复内容吗?或者您只想使用您拥有的值再次创建该文件?@Codebender-由于系统崩溃,如果删除了文件Seq.properties,那么我想在与上一个值相同的位置创建一个文件。假设如果Trans_no=99和reg_id=1,那么现在如果系统崩溃,则应生成一个文件,并应保存值Trans_no=99和reg_id=1 Hanks,但使用数据库的成本更高。这是实时场景OK。然后它就像将文件存储在本地和服务器中一样。当您尝试从文件中检索值时,如果文件不存在,则从服务器复制文件并从中获取值。如果负载过大,以至于数据库查询成本非常重要,则基于文件的解决方案将无法工作。您将锁定该文件,或者多个客户端将同时写入该文件。数据库是解决您的问题的正确答案。