文件IO-Properties

java.lang.Object
继承者 java.util.Dictionary
继承者 java.util.Hashtable
继承者 java.util.Properties
所有已实现的接口:
Serializable, Cloneable, Map

1.构造方法
Properties();
Properties(Properties default):创建一个带有默认值的空属性列表
2.主要方法:
getProperty(String key)通过指定的键获取属性,返回的是一个字符串
load(InputStream inStream)从输入流中读取属性列表
load(Reader reader)按简单的面向行的格式从输入字符流中读取属性列表
setProperty(String key,String value),向列表中输入键值对
store(OutputStream os,String commit)将此 Properties 表中的属性列表(键和元素对)写入输出流,commit为注释
store(Writer writer,String commit)将此 Properties 表中的属性列表(键和元素对)写入输出字符
stringPropertyNames();返回一个set集合的键值对字符串

public class Demo09 {
    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.setProperty("zsh", "1");
        properties.setProperty("zms","2");
        properties.setProperty("sks","3");
        Set<String> strings = properties.stringPropertyNames();
        for(String key:strings){
            String value = properties.getProperty(key);
            System.out.println(key+"="+value);
        }
    }
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class Demo10 {
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        properties.setProperty("sks","180");
        properties.setProperty("zsh","170");
        FileWriter fileWriter = new FileWriter("G:" + File.separator + "demo.txt");
        properties.store(fileWriter,"sava data");
        fileWriter.close();
    }
}

你可能感兴趣的:(IO-Properties,java)