在Java中,可以使用java.util.Properties
類來讀取property文件。
以下是讀取property文件的步驟:
Properties
對象:Properties properties = new Properties();
load()
方法加載property文件:try {
FileInputStream fileInputStream = new FileInputStream("path/to/your/file.properties");
properties.load(fileInputStream);
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
getProperty()
方法獲取屬性值:String value = properties.getProperty("key");
完整的示例代碼:
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class ReadPropertyFile {
public static void main(String[] args) {
Properties properties = new Properties();
try {
FileInputStream fileInputStream = new FileInputStream("path/to/your/file.properties");
properties.load(fileInputStream);
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
String value = properties.getProperty("key");
System.out.println(value);
}
}
確保將"path/to/your/file.properties"替換為你實際的property文件路徑。