如何通过java代码在属性文件中写入值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22370051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:19:37  来源:igfitidea点击:

How to write values in a properties file through java code

java

提问by user3411418

I have an issue.

我有一个问题。

I have a properties file. I want to store some values in that file and will implement in the code whenever it is required. Is there any way to do that?

我有一个属性文件。我想在该文件中存储一些值,并在需要时在代码中实现。有没有办法做到这一点?

I am using Propertiesclass to do that..

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

回答by Subhrajyoti Majumder

Load the properties file using java.util.Properties.

使用java.util.Properties.

Code snippet -

代码片段 -

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

It provides Properties#setProperty(java.lang.String, java.lang.String)which helps to add new property.

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

Code snippet -

代码片段 -

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

This new set you can save using Properties#store(java.io.OutputStream, java.lang.String)

您可以使用此新设置保存 Properties#store(java.io.OutputStream, java.lang.String)

Code Snippet -

代码片段 -

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

回答by Devavrata

You can do it in following way:

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

  1. Set the properties first in the Propertiesobject by using object.setProperty(String obj1, String obj2).

  2. Then write it to your Fileby passing a FileOutputStreamto properties_object.store(FileOutputStream, String).

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

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

Here is the example code :

这是示例代码:

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);
    }
}

回答by VyTcdc

This work for me.

这对我有用。

    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);
                    }

回答by Mehdi

Your problem is not clear since Writing/reading from properties files is something already available in java.

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

To write to properties file you can use the Properties class as you mentioned :

要写入属性文件,您可以使用您提到的 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

来源和更多例子在这里