Java properties文件用法

package com.suyang.properties;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

public class TestProperties {

	private final String PATH = "test.properties";
	private static Properties props = new Properties();

	static {
		try {
			props.load(new FileInputStream("test.properties"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String getProperty(String name) {
		return props.getProperty(name);
	}

	public void setProperty(String name, String value) {
		props.setProperty(name, value);
	}
	
	public void save(){
		OutputStream fos = null;
		try {
			fos = new FileOutputStream(PATH);
			props.store(fos, "");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}  finally{
			if(fos != null){
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

  

posted @ 2014-06-06 13:35  Xsi64  阅读(269)  评论(0编辑  收藏  举报