propertiesクラスを使用して、javaで設定ファイルを読み込む方法は何ですか?

Javaで、設定ファイルを読み込むためにPropertiesクラスを使用することができます。以下は簡単な例です:

  1. 設定ファイル.properties
name=John
age=25
  1. 性質
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class Main {
    public static void main(String[] args) {
        Properties properties = new Properties();
        FileInputStream input = null;

        try {
            input = new FileInputStream("config.properties");
            properties.load(input);

            // 读取配置文件中的属性值
            String name = properties.getProperty("name");
            String age = properties.getProperty("age");

            System.out.println("name: " + name);
            System.out.println("age: " + age);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

上記のコードでは、最初にPropertiesオブジェクトを作成し、次にFileInputStreamを使用して設定ファイルを読み込みます。その後、load()メソッドを使用して設定ファイルをPropertiesオブジェクトに読み込みます。最後に、getProperty()メソッドを使用して設定ファイルからプロパティ値を取得します。

bannerAds