Java-util-properties-load-stream

提供:Dev Guides
移動先:案内検索

Java.util.Properties.load()メソッド

説明

  • java.util.Properties.load(InputStream inStream)*メソッドは、入力バイトストリームからプロパティリスト(キーと要素のペア)を読み取ります。 入力ストリームは、load(Reader)で指定された単純な行指向の形式であり、ISO 8859-1文字エンコードを使用すると想定されています。つまり、各バイトは1つのLatin1文字です。

宣言

以下は* java.util.Properties.load()*メソッドの宣言です

public void load(InputStream inStream)

パラメーター

*inStream* -入力ストリーム。

戻り値

このメソッドは値を返しません。

例外

  • IOException -入力ストリームからの読み取り中にエラーが発生した場合。
  • IllegalArgumentException -入力ストリームに不正なUnicodeエスケープシーケンスが含まれる場合。

次の例は、java.util.Properties.list()メソッドの使用法を示しています。

package com.finddevguides;

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