讀取java配置文件

package com.io.study;

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

public class TestProperties {
	
	//創建配置文件
	Properties pp = new Properties();
	
	
	/*
	 * 向配置文件中寫入內容
	 */
	public void pushPro(){
		FileOutputStream fos = null;
		
		pp.setProperty("1", "你是誰?");
		pp.setProperty("2", "who are you?");
		
		try {
			fos = new FileOutputStream("d:/test.properties");
			pp.store(fos, "test");
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("store successful!");
	}
	
	
	/*
	 *讀取配置文件中的信息 
	 */
	public void getPro(){
		FileInputStream fis = null;
		try {
			fis = new FileInputStream("d:/test.properties");
			pp.load(fis);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(pp.getProperty("1"));
		System.out.println(pp.getProperty("2"));
	}
	
	
	/*
	 * 方法入口
	 */
	public static void main(String[] args) {
		TestProperties tp = new TestProperties();
		//tp.pushPro();
		tp.getPro();
	}
}
發佈了41 篇原創文章 · 獲贊 8 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章