【问题标题】:How to write values in a properties file through java code如何通过java代码在属性文件中写入值
【发布时间】:2014-03-13 05:45:35
【问题描述】:

我有一个问题。

我有一个属性文件。我想在该文件中存储一些值,并在需要时在代码中实现。有什么办法吗?

我正在使用Properties 类来做到这一点..

【问题讨论】:

标签: java


【解决方案1】:

使用java.util.Properties 加载属性文件。

代码 sn-p -

Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream("xyz.properties");
prop.load(in);

它提供Properties#setProperty(java.lang.String, java.lang.String) 有助于添加新属性。

代码 sn-p -

prop.setProperty("newkey", "newvalue");

你可以使用Properties#store(java.io.OutputStream, java.lang.String)保存这个新集合

代码片段 -

prop.store(new FileOutputStream("xyz.properties"), null);

【讨论】:

  • 感谢编辑并使答案更有意义@Jason C
  • 这两种情况下的文件xyz.properties(InputStream 和 FileOutputStream)在这里是否相同?
  • 是的,这两种情况都是一样的。
  • 对我来说(在战争中),它使用 'getClass().getClassLoader().getResourceAsStream' 而不是 'getClass().getResourceAsStream'
  • 这是个坏主意,因为它会弄乱编码
【解决方案2】:

您可以通过以下方式进行:

  1. 首先使用object.setProperty(String obj1, String obj2)Properties对象中设置属性。

  2. 然后通过将FileOutputStream 传递给properties_object.store(FileOutputStream, String) 将其写入您的File

这里是示例代码:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;

class Main
{
    static File file;
    static void saveProperties(Properties p) throws IOException
    {
        FileOutputStream fr = new FileOutputStream(file);
        p.store(fr, "Properties");
        fr.close();
        System.out.println("After saving properties: " + p);
    }

    static void loadProperties(Properties p)throws IOException
    {
        FileInputStream fi=new FileInputStream(file);
        p.load(fi);
        fi.close();
        System.out.println("After Loading properties: " + p);
    }

    public static void main(String... args)throws IOException
    {
        file = new File("property.dat");
        Properties table = new Properties();
        table.setProperty("Shivam","Bane");
        table.setProperty("CS","Maverick");
        System.out.println("Properties has been set in HashTable: " + table);
        // saving the properties in file
        saveProperties(table);
        // changing the property
        table.setProperty("Shivam", "Swagger");
        System.out.println("After the change in HashTable: " + table);
        // saving the properties in file
        saveProperties(table);
        // loading the saved properties
        loadProperties(table);
    }
}

【讨论】:

  • 我收到此错误消息:“资源与文件系统不同步:'xyz.properties'。”以及如何删除我的工作结束的文件......在检索值时,值是空......我想知道可能是什么问题?
  • 对于“Resources is out of syn”检查link。对于删除简单使用删除函数file.delete()
  • 这会保存到属性文件中,但会从文件中删除存在数据。
【解决方案3】:

您的问题不清楚,因为从属性文件中写入/读取在 java 中已经可用。

要写入属性文件,您可以使用前面提到的 Properties 类:

Properties properties = new Properties();
try(OutputStream outputStream = new FileOutputStream(PROPERTIES_FILE_PATH)){  
    properties.setProperty("prop1", "Value1");
    properties.setProperty("prop2", "Value2");
    properties.store(outputStream, null);
} catch (IOException e) {
    e.printStackTrace();
} 

Source and more examples here

【讨论】:

    【解决方案4】:

    这对我有用。

        Properties prop = new Properties();
    
                    try {
                            InputStream in = new FileInputStream("src/loop.properties");
                            prop.load(in);
                        } catch (IOException ex) {
                           System.out.println(ex);
                        }
               //Setting the value to  our properties file.
               prop.setProperty("LOOP", "1");
               //Getting the value from  our properties file.
               String value = prop.getProperty("LOOP").trim();
    
                    try {
                            prop.store(new FileOutputStream("src/loop.properties"), null);
                        } catch (IOException ex) {
                           System.out.println(ex);
                        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-31
      • 2019-10-25
      • 1970-01-01
      • 1970-01-01
      • 2018-06-15
      • 2012-01-06
      相关资源
      最近更新 更多