- 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
广告