`
ctrain
  • 浏览: 133551 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

读写Poperties文件

阅读更多
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Properties;
import com.jeecms.download.entity.LilvEntity;

public class LilvManageDao {
	public LilvEntity queryLilv() throws IOException {

		ClassLoader classLoader = Thread.currentThread()
				.getContextClassLoader();
		if (classLoader == null) {
			classLoader = this.getClass().getClassLoader();
		}
		URL inputUrl = classLoader.getResource("a.properties");
		File file = new File(inputUrl.getPath());
		InputStream in = new BufferedInputStream(new FileInputStream(inputUrl
				.getPath()));
		Properties p = new Properties();
		p.load(in);
		LilvEntity li = new LilvEntity();
		li.setGjj_1_3(p.getProperty("gjj_1_3"));
		li.setGjj_3_5(p.getProperty("gjj_3_5"));
		li.setGjj_5__(p.getProperty("gjj_5__"));
		li.setDk_1_3(p.getProperty("dk_1_3"));
		li.setDk_3_5(p.getProperty("dk_3_5"));
		li.setDk_5__(p.getProperty("dk_5__"));
		li.setCalc_num(p.getProperty("calc_num"));
		in.close();
		return li;
	}

	/**
	 * 更新properties文件的键值对 如果该主键已经存在,更新该主键的值;如果该主键不存在,则插件一对键值。
	 * 
	 * @param keyname
	 *            键名
	 *@param keyvalue
	 *            键值
	 */
	public void updateProperties(String keyname, String keyvalue) {
		ClassLoader classLoader = Thread.currentThread()
				.getContextClassLoader();
		if (classLoader == null) {
			classLoader = this.getClass().getClassLoader();
		}
		URL inputUrl = classLoader.getResource("a.properties");
		File file = new File(inputUrl.getPath());
		try {
			Properties props = new Properties();
			props.load(new FileInputStream(inputUrl.getPath()));
			// 调用 Hashtable 的方法 put,使用 getProperty 方法提供并行性。
			// 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
			OutputStream fos = new FileOutputStream(inputUrl.getPath());
			props.setProperty(keyname, keyvalue);
			// 以适合使用 load 方法加载到 Properties 表中的格式,
			// 将此 Properties 表中的属性列表(键和元素对)写入输出流
			props.store(fos, "Update '" + keyname + "' value");
			fos.flush();
		} catch (IOException e) {
			System.err.println("属性文件更新错误");
		}
	}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics