Properties是Hashtable的子类

import java.io.*;
import java.util.*;

public class PropertiesExample {
	public static void main(String args[]) {
		try {

			Properties grades = new Properties();

			grades.setProperty("Geometry", "20");
			grades.setProperty("Algebra", "20");
			grades.setProperty("Physics", "18");
			grades.setProperty("Chemistry", "17");
			grades.setProperty("Biology", "19");

			// Save the grades properties using store() and an output stream
			FileOutputStream out = new FileOutputStream(
					"d:\\schoolGrades.properties");
			grades.store(out, null);
			out.close();

			// load the properties file using load() and an input stream
			FileInputStream in = new FileInputStream("d:\\schoolGrades.properties");
			grades.load(in);
			in.close();

			// iterate properties file to get key-value pairs
			for (String key : grades.stringPropertyNames()) {
				String value = grades.getProperty(key);
				System.out.println("The grade in " + key + " is: " + value);
			}

			// search for key-value pair not in the list
			// you must define a default value, so as to return it
			// in case the key is not found in the main list
			String str = grades.getProperty("History", "No grade");
			System.out.println("The grade in History is " + str);

		} catch (IOException e) {

			e.printStackTrace();
		}
	}
}

写入的文件中的内容:

#Thu Jan 16 18:29:41 CST 2014
Physics=18
Biology=19
Chemistry=17
Algebra=20
Geometry=20

原文:http://examples.javacodegeeks.com/core-java/util/properties/java-util-properties-example/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值