如果我将属性文件声明为 config.properties,如何从文件夹外部读取属性文件



我创建了属性文件,比如config.properties.现在我想知道有关如何从 java 中的属性文件中读取参数值的过程。

在 eclipse 中使用 jdk1.7。

请参阅下面的代码来读取 config.properties 文件。属性文件可以是文件系统中的任何位置。您必须为属性文件提供正确的绝对路径。

import java.io.*;
import java.util.Properties;
class ConfigReader
{
    public static void main(String args[])
    {
        try
        {
            InputStream inputstream;
            Properties prop = new Properties();
            inputstream = new FileInputStream("pathtofile\config.properties");
            if(inputstream!=null)
            {
                prop.load(inputstream);
                String property1 = prop.getProperty("property1");
                String property2 = prop.getProperty("property2");
                System.out.println(property1+"n"+property2);
            }
            else
            {
                System.out.println("File not found");
            }
        }
        catch(Exception e)
        {
            System.out.println("Error");
        }
    }

}

最新更新