- Java.util 包類
- Java.util - 首頁
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util 包擴充套件
- Java.util - 介面
- Java.util - 異常
- Java.util - 列舉
- Java.util 有用資源
- Java.util - 有用資源
- Java.util - 討論
Java Properties load() 方法
描述
Java Properties load(InputStream inStream) 方法從輸入位元組流中讀取屬性列表(鍵值對)。輸入流採用簡單的行格式,如 load(Reader) 中所指定,並假設使用 ISO 8859-1 字元編碼;即每個位元組是一個 Latin1 字元。
宣告
以下是 java.util.Properties.load() 方法的宣告
public void load(InputStream inStream)
引數
inStream - 輸入流。
返回值
此方法不返回值。
異常
IOException - 如果在從輸入流讀取時發生錯誤。
IllegalArgumentException - 如果輸入流包含格式錯誤的 Unicode 轉義序列。
Java Properties load(Reader reader) 方法
描述
java.util.Properties.load(Reader reader) 方法以簡單的行格式從輸入字元流中讀取屬性列表(鍵值對)。
宣告
以下是 java.util.Properties.load(Reader reader) 方法的宣告
public void load(Reader reader)
引數
reader - 輸入字元流。
返回值
此方法不返回值。
異常
IOException - 如果在從輸入流讀取時發生錯誤。
IllegalArgumentException - 如果輸入流包含格式錯誤的 Unicode 轉義序列。
從 TXT 檔案載入屬性示例
以下示例演示瞭如何使用 Java Properties load(InputStream) 方法從流中載入屬性。
package com.tutorialspoint;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class PropertiesDemo {
public static void main(String[] args) {
Properties prop = new Properties();
String s = "Height=200";
String s2 = "Width=15";
try {
// create a new input and output stream
FileOutputStream fos = new FileOutputStream("properties.txt");
FileInputStream fis = new FileInputStream("properties.txt");
// write the first property in the output stream file
fos.write(s.getBytes());
// change the line between the two properties
fos.write("\n".getBytes());
// write next property
fos.write(s2.getBytes());
// load from input stream
prop.load(fis);
// print the properties list from System.out
prop.list(System.out);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
輸出
讓我們編譯並執行以上程式,這將產生以下結果:
-- listing properties -- Width=15 Height=200
從字串載入屬性示例
以下示例演示瞭如何使用 Java Properties load(Reader) 方法從讀取器中載入屬性。
package com.tutorialspoint;
import java.io.IOException;
import java.io.StringReader;
import java.util.Properties;
public class PropertiesDemo {
public static void main(String[] args) {
Properties prop = new Properties();
String s = "Height=200\nWidth=15";
// create a new reader
StringReader reader = new StringReader(s);
try {
// load from input stream
prop.load(reader);
// print the properties list from System.out
prop.list(System.out);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
輸出
讓我們編譯並執行以上程式,這將產生以下結果:
-- listing properties -- Width=15 Height=200
java_util_properties.htm
廣告