Java Properties load()方法

java.util.Properties.load(InputStream inStream) 从InputStream对象加载数据到属性列表

1 语法

public void load(InputStream inStream)

2 参数

inStream:输入流。

3 返回值

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.Properties.load(InputStream inStream)方法的例子
 */
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

public class Demo {
    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

热门文章

优秀文章