在Java應用程序中,經常需要讀取properties配置文件中的參數,以便配置應用程序的運行參數。讀取properties可以使用Java的 Properties 類,該類可以很方便地讀取配置文件中的屬性。
一、讀取properties文件基礎
首先,我們需要先創建一個 properties 文件,包含需要讀取的屬性和值:
name=張三 age=25 gender=男
然後,我們需要使用 Properties 類來讀取配置文件。我們可以使用 Properties 類提供的 load() 方法來載入 properties 文件。
import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ReadProperties { public static void main(String[] args) { // 獲取配置文件路徑 String configPath = "config.properties"; // 創建 Properties 對象 Properties props = new Properties(); // 使用 ClassLoader 載入配置文件 InputStream in = ReadProperties.class.getClassLoader().getResourceAsStream(configPath); try { // 載入配置文件 props.load(in); } catch (IOException e) { e.printStackTrace(); } // 獲取屬性值 String name = props.getProperty("name"); String age = props.getProperty("age"); String gender = props.getProperty("gender"); System.out.println("name=" + name); System.out.println("age=" + age); System.out.println("gender=" + gender); } }
二、讀取properties文件的其他方式
1. 使用 Properties 類提供的 load(InputStream) 方法
我們可以使用 Properties 類提供的 load(InputStream) 方法,直接從 InputStream 中讀取配置文件。
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ReadProperties { public static void main(String[] args) { // 獲取配置文件路徑 String configPath = "config.properties"; // 創建 Properties 對象 Properties props = new Properties(); // 使用 FileInputStream 載入配置文件 InputStream in = null; try { in = new FileInputStream(configPath); // 載入配置文件 props.load(in); } catch (IOException e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } } // 獲取屬性值 String name = props.getProperty("name"); String age = props.getProperty("age"); String gender = props.getProperty("gender"); System.out.println("name=" + name); System.out.println("age=" + age); System.out.println("gender=" + gender); } }
2. 使用 Properties 類提供的 loadFromXML(InputStream) 方法
除了可以讀取 properties 配置文件之外,Properties 類還提供了讀取 XML 格式的配置文件的方法。
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ReadProperties { public static void main(String[] args) { // 獲取配置文件路徑 String configPath = "config.xml"; // 創建 Properties 對象 Properties props = new Properties(); // 使用 FileInputStream 載入配置文件 InputStream in = null; try { in = new FileInputStream(configPath); // 載入配置文件 props.loadFromXML(in); } catch (IOException e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } } // 獲取屬性值 String name = props.getProperty("name"); String age = props.getProperty("age"); String gender = props.getProperty("gender"); System.out.println("name=" + name); System.out.println("age=" + age); System.out.println("gender=" + gender); } }
3. 使用 ResourceBundle 類讀取配置文件
另一種讀取配置文件的方式是使用 ResourceBundle 類。通過 ResourceBundle 類,我們可以讀取多種類型的配置文件,如 properties、XML、txt 等等。
import java.util.ResourceBundle; public class ReadProperties { public static void main(String[] args) { // 獲取配置文件路徑 String configPath = "config"; // 創建 ResourceBundle 對象 ResourceBundle bundle = ResourceBundle.getBundle(configPath); // 獲取屬性值 String name = bundle.getString("name"); String age = bundle.getString("age"); String gender = bundle.getString("gender"); System.out.println("name=" + name); System.out.println("age=" + age); System.out.println("gender=" + gender); } }
三、讀取properties文件中的數組
在 properties 配置文件中,可以將多個值用逗號隔開存儲,以實現讀取數組的效果。
fruits=apple,banana,orange
在讀取數組時,我們需要先使用 getProperty() 方法獲取一個包含了所有值的字元串,然後再使用 split() 方法分割字元串,得到一個字元串數組。
import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.Properties; public class ReadPropertiesArray { public static void main(String[] args) { // 獲取配置文件路徑 String configPath = "config.properties"; // 創建 Properties 對象 Properties props = new Properties(); // 使用 ClassLoader 載入配置文件 InputStream in = ReadPropertiesArray.class.getClassLoader().getResourceAsStream(configPath); try { // 載入配置文件 props.load(in); } catch (IOException e) { e.printStackTrace(); } // 獲取屬性值 String fruitsStr = props.getProperty("fruits"); String[] fruits = fruitsStr.split(","); System.out.println(Arrays.toString(fruits)); } }
四、讀取properties文件中的特殊字元
在 properties 配置文件中,有時候需要使用特殊字元,如空格、等號、冒號等等。這些特殊字元在 properties 文件中需要進行轉義。
在 Java 中,可以使用 Unicode 轉義進行轉義。例如:空格字元可以寫成 \u0020。
name=John\u0020Doe password=my\u0020password
在讀取特殊字元時,Properties 類會自動解析 Unicode 轉義字元。
import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ReadPropertiesSpecialChar { public static void main(String[] args) { // 獲取配置文件路徑 String configPath = "config.properties"; // 創建 Properties 對象 Properties props = new Properties(); // 使用 ClassLoader 載入配置文件 InputStream in = ReadPropertiesSpecialChar.class.getClassLoader().getResourceAsStream(configPath); try { // 載入配置文件 props.load(in); } catch (IOException e) { e.printStackTrace(); } // 獲取屬性值 String name = props.getProperty("name"); String password = props.getProperty("password"); System.out.println("name=" + name); System.out.println("password=" + password); } }
五、讀取properties文件中的中文
在 properties 配置文件中,可以使用中文字元。但是,這些中文字元在文件中是以 Unicode 編碼進行存儲的。
在讀取中文字元時,Properties 類會自動解析 Unicode 編碼,將其轉換成中文字元。
name=\u5f20\u4e09
import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ReadPropertiesChinese { public static void main(String[] args) { // 獲取配置文件路徑 String configPath = "config.properties"; // 創建 Properties 對象 Properties props = new Properties(); // 使用 ClassLoader 載入配置文件 InputStream in = ReadPropertiesChinese.class.getClassLoader().getResourceAsStream(configPath); try { // 載入配置文件 props.load(in); } catch (IOException e) { e.printStackTrace(); } // 獲取屬性值 String name = props.getProperty("name"); System.out.println("name=" + name); } }
六、總結
本文介紹了如何使用 Properties 類讀取配置文件。讀取配置文件可以使用不同的方式,包括使用 Properties 類提供的 load()、load(InputStream) 和 loadFromXML(InputStream) 方法、使用 ResourceBundle 類等等。同時,讀取properties文件中的數組、特殊字元、中文字元也有一定的技巧。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/297550.html