天天看點

java 讀取properties配置檔案的幾種方式

作者:Clover幸運之星

在 Java 中,有幾種方式可以讀取 properties 配置檔案。除了之前提到的使用 java.util.Properties 類,還有其他一些方式可以實作相同的目标。以下是幾種常見的讀取 properties 配置檔案的方式:

  1. 使用 Properties 類:

這是最常見的方式,使用 java.util.Properties 類來讀取和操作 properties 配置檔案;

你可以使用 java.util.Properties 類來讀取和操作 properties 配置檔案。Properties 類可以用于加載和儲存鍵值對形式的配置資訊。以下是一個簡單的示例,示範如何讀取 properties 配置檔案:

假設你有一個 config.properties 檔案,内容如下:

propertiesCopy code
# config.properties
database.url=jdbc:mysql://localhost:3306/mydb
database.username=myuser
database.password=mypassword           

下面是使用 java.util.Properties 類讀取這個配置檔案的示例代碼:

javaCopy code
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertiesReader {

    public static void main(String[] args) {
        Properties properties = new Properties();

        try {
            // 從檔案加載配置
            FileInputStream fileInputStream = new FileInputStream("path/to/config.properties");
            properties.load(fileInputStream);
            fileInputStream.close();

            // 擷取配置值
            String dbUrl = properties.getProperty("database.url");
            String dbUsername = properties.getProperty("database.username");
            String dbPassword = properties.getProperty("database.password");

            System.out.println("Database URL: " + dbUrl);
            System.out.println("Database Username: " + dbUsername);
            System.out.println("Database Password: " + dbPassword);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}           

請将 path/to/config.properties 替換為實際的配置檔案路徑。

在這個示例中,我們使用 FileInputStream 來加載 properties 配置檔案,然後使用 load 方法将其内容加載到 Properties 對象中。接着,我們可以使用 getProperty 方法來擷取配置值。

請注意,使用 Properties 類還可以用于寫入和儲存 properties 配置檔案。如果你需要修改配置并将其儲存回檔案中,可以使用 setProperty 方法和 store 方法。

總之,java.util.Properties 類提供了一種友善的方式來讀取和操作 properties 配置檔案中的鍵值對資訊。

2.使用 ResourceBundle 類:

ResourceBundle 是 Java 标準庫中的另一種用于讀取屬性檔案的方式,它更多地用于本地化和國際化。這種方式适用于加載位于類路徑中的屬性檔案。

javaCopy code
import java.util.ResourceBundle;

public class ResourceBundleExample {

    public static void main(String[] args) {
        ResourceBundle bundle = ResourceBundle.getBundle("config"); // 無需檔案擴充名
        String dbUrl = bundle.getString("database.url");
        String dbUsername = bundle.getString("database.username");
        String dbPassword = bundle.getString("database.password");

        System.out.println("Database URL: " + dbUrl);
        System.out.println("Database Username: " + dbUsername);
        System.out.println("Database Password: " + dbPassword);
    }
}           

3.使用 Spring 的 PropertyPlaceholderConfigurer:

如果你使用 Spring 架構,你可以使用 PropertyPlaceholderConfigurer 類來加載和解析屬性檔案中的配置。這對于在 Spring 應用程式中管理配置非常有用。

xmlCopy code
<!-- 在 Spring 配置檔案中配置 PropertyPlaceholderConfigurer -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:config.properties" />
</bean>           

然後,在 Spring 的 bean 中可以直接使用占位符 ${} 來引用屬性值。

xmlCopy code
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.username}" />
    <property name="password" value="${database.password}" />
</bean>           

4.使用 Apache Commons Configuration 庫:

Apache Commons Configuration 是一個用于讀取各種配置格式(包括 properties 檔案)的庫,提供了更靈活和功能豐富的配置管理。

javaCopy code
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.builder.fluent.Configurations;

public class CommonsConfigurationExample {

    public static void main(String[] args) {
        Configurations configs = new Configurations();
        try {
            Configuration config = configs.properties(new File("path/to/config.properties"));

            String dbUrl = config.getString("database.url");
            String dbUsername = config.getString("database.username");
            String dbPassword = config.getString("database.password");

            System.out.println("Database URL: " + dbUrl);
            System.out.println("Database Username: " + dbUsername);
            System.out.println("Database Password: " + dbPassword);
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
    }
}           

以上是一些常見的讀取 properties 配置檔案的方式。根據你的項目需求和技術棧,選擇最适合你的方法進行配置檔案讀取。