共計 1343 個字符,預計需要花費 4 分鐘才能閱讀完成。
在 Java 中調用 property 文件有多種方法,其中常用的方法有以下幾種:
-
使用
java.util.Properties類:import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; public class PropertyFileExample {public static void main(String[] args) {Properties properties = new Properties(); try {FileInputStream fileInputStream = new FileInputStream("path/to/your/property/file.properties"); properties.load(fileInputStream); fileInputStream.close();} catch (IOException e) {e.printStackTrace(); } String value = properties.getProperty("key"); System.out.println("Value: " + value); } } -
使用
java.util.ResourceBundle類:import java.util.ResourceBundle; public class PropertyFileExample {public static void main(String[] args) {ResourceBundle resourceBundle = ResourceBundle.getBundle("your.property.file"); String value = resourceBundle.getString("key"); System.out.println("Value: " + value); } } -
使用
java.nio.file.Files類:import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; public class PropertyFileExample {public static void main(String[] args) {Path filePath = Paths.get("path/to/your/property/file.properties"); try {List<String> lines = Files.readAllLines(filePath); for (String line : lines) {if (line.startsWith("key=")) {String value = line.substring(4); System.out.println("Value: " + value); break; } } } catch (IOException e) {e.printStackTrace(); } } }
請根據自己的需求選擇適合的方法來調用 property 文件。
丸趣 TV 網 – 提供最優質的資源集合!
正文完
發表至: Java
2023-12-12