共計 911 個字符,預(yù)計需要花費 3 分鐘才能閱讀完成。
在 Java 中,可以使用 java.util.Properties
類來讀取 properties 文件的值。
以下是一個簡單的示例:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ReadPropertiesExample {public static void main(String[] args) {Properties properties = new Properties();
InputStream inputStream = null;
try {// 讀取 properties 文件
inputStream = new FileInputStream("config.properties");
properties.load(inputStream);
// 讀取屬性值
String value1 = properties.getProperty("key1");
String value2 = properties.getProperty("key2");
System.out.println("Value 1: " + value1);
System.out.println("Value 2: " + value2);
} catch (IOException e) {e.printStackTrace();
} finally {if (inputStream != null) {try {inputStream.close();
} catch (IOException e) {e.printStackTrace();
}
}
}
}
}
在這個示例中,我們首先創(chuàng)建一個 Properties
對象,然后使用 FileInputStream
來讀取 properties 文件。接下來,使用 load()
方法加載文件內(nèi)容到 Properties
對象中。最后,可以使用 getProperty()
方法來獲取具體的屬性值。
需要注意的是,要根據(jù)實際的 properties 文件路徑來指定 FileInputStream
的參數(shù)。
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完
發(fā)表至: Java
2023-12-13