Java 类集简介(Properties子类)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ikv1989/article/details/79960524

Properties是Hashtable子类

Properties类主要有以下操作方法:

·public Object setProperties(String key,String value);

· public String getProperties(String key);

· public String getProperties(String key,String defaultValue) ;

范例:

import java.util.Properties;

public class Demo {
	public static void main(String[] args) throws Exception {
		Properties pro = new Properties();
		pro.setProperty("GZ", "广州");
		pro.setProperty("SZ", "深圳");
		System.out.println(pro.getProperty("GZ"));
		System.out.println(pro.getProperty("SZ"));
		System.out.println(pro.getProperty("BJ", "信息不存在"));
	}
}

==============分割线 ==============

在Properties类里面提供有数据输出的操作:public void store(OutputStream out,String comments)throws IOException;

范例:将属性信息保存在文件里:

import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;

public class Demo {
	public static void main(String[] args) throws Exception {
		Properties pro = new Properties();
		pro.setProperty("GZ", "广州");
		pro.setProperty("SZ", "深圳");
		// 一般后缀可以任意设置,但是标准上,属性文件必须以*.properties,为了和国际化对应
		pro.store(new FileOutputStream("e:" + File.separator + "area.properties"), "Area Info");
	}
}

==============分割线 ==============

可以从指定输入流中读取属性信息:public void load(InputStream inStream)throws IOException

范例

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

public class Demo {
	public static void main(String[] args) throws Exception {
		Properties pro = new Properties();
		pro.load(new FileInputStream("e:" + File.separator + "area.properties"));
		System.out.println(pro.getProperty("GZ"));
	}
}

阅读更多

没有更多推荐了,返回首页