目录

void load(InputStream inStream)

描述 (Description)

java.util.Properties.load(InputStream inStream)方法从输入字节流中读取属性列表(键和元素对)。 输入流采用加载(Reader)中指定的简单的面向行的格式,并假定使用ISO 8859-1字符编码; 即每个字节是一个Latin1字符。

声明 (Declaration)

以下是java.util.Properties.load()方法的声明

public void load(InputStream inStream)

参数 (Parameters)

inStream - 输入流。

返回值 (Return Value)

此方法不返回值。

异常 (Exception)

  • IOException - 如果从输入流中读取时发生错误。

  • IllegalArgumentException - 如果输入流包含格式错误的Unicode转义序列。

例子 (Example)

以下示例显示了java.util.Properties.list()方法的用法。

package com.iowiki;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
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
↑回到顶部↑
WIKI教程 @2018