Java 写入属性文件无效

Java Writing to Properties File Not Working

出于某种原因,以下代码(在我的程序中被 try-catch 语句包围)不起作用,因为它继续抛出 NumberFormatException, 并且还擦除属性文件中的信息。感谢帮助。

        File propFile = new File("path\to\file\properties.properties");
        FileOutputStream outStream  = new FileOutputStream(propFile);
        FileInputStream inStream = new FileInputStream(propFile);

        Properties prop = new Properties();
        prop.load(inStream);

        out.println(prop.getProperty("entryID"));

        prop.setProperty("entryID", Integer.toString(Integer.parseInt(prop.getProperty("entryID"))+1));

        prop.store(outStream, "");

只需更改序列行,您在读取内容之前创建 FileOutStream 对象

请查找附加的工作片段

文件 propFile = 新文件("path\to\file\properties.properties");

    FileInputStream inStream = new FileInputStream(propFile);

    Properties prop = new Properties();
    prop.load(inStream);

    System.out.println(prop.getProperty("entryID"));

    prop.setProperty("entryID", Integer.toString(Integer.parseInt(prop.getProperty("entryID"))+1));
    FileOutputStream outStream  = new FileOutputStream(propFile);
    prop.store(outStream, "");