Java读取配置文件

要读取保存在配置文件中的配置信息,可以使用Java提供的API。以下是一个简单的示例:

import java.io.IOException;
import java.util.Properties;

public class ReadConfiguration {

    public static void main(String[] args) {

        // 创建 Properties 对象
        Properties properties = new Properties();

        // 从文件中加载配置信息
        try {
            properties.load(new FileInputStream("config.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 获取配置信息
        String name = properties.getProperty("name");
        int age = Integer.parseInt(properties.getProperty("age"));

        // 打印配置信息
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }
}
zip 文件大小:3.25KB