Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件。

它提供了几个主要的方法:

1. getProperty ( String key),用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。

2. load ( InputStream inStream),从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。

3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。

4. store ( OutputStream out, String comments),以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。

5. clear (),清除所有装载的 键 - 值对。该方法在基类中提供。

 package org.meter.demo.properties;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger; public class PropertiesTest { private static Logger logger = Logger.getLogger(PropertiesTest.class.getName());
private PropertiesTest() {
}
/**
* 读取配置文件某属性
*/
public static String readValue(String filePath, String key) {
Properties props = new Properties();
try {
// 注意路径以 / 开始,没有则处理
if (!filePath.startsWith("/"))
filePath = "/" + filePath;
InputStream in = PropertiesTest.class.getResourceAsStream(filePath);
props.load(in);
String value = props.getProperty(key);
return value;
} catch (Exception e) { logger.log(Level.WARNING,e.getMessage());
return null;
}
}
/**
* 打印配置文件全部内容(filePath,配置文件名,如果有路径,props/test.properties)
*/
public static void readProperties(String filePath) {
Properties props = new Properties();
try {
// 注意路径以 / 开始,没有则处理
if (!filePath.startsWith("/"))
filePath = "/" + filePath;
InputStream in = PropertiesTest.class.getResourceAsStream(filePath);
props.load(in);
Enumeration<?> en = props.propertyNames();
// 遍历打印
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = props.getProperty(key);
logger.log(Level.INFO, key + ":" + Property);
}
} catch (Exception e) {
logger.log(Level.WARNING, e.getMessage());
}
}
/**
* 将值写入配置文件
*/
public static void writeProperties(String fileName, String parameterName, String parameterValue) throws Exception {
// 本地测试特别注意,如果是maven项目,请到\target目录下查看文件,而不是源代码下
// 注意路径不能加 / 了,加了则移除掉
if (fileName.startsWith("/"))
fileName.substring(1);
String filePath = PropertiesTest.class.getResource("/").getPath()+fileName;
// 获取配置文件
Properties pps = new Properties();
File file = new File(filePath);
if(!file.exists()){
file.createNewFile();
}
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
pps.load(in);
in.close();
OutputStream out = new FileOutputStream(filePath); // 设置配置名称和值
pps.setProperty(parameterName, parameterValue);
// comments 等于配置文件的注释
pps.store(out, "Update " + parameterName + " name");
out.flush();
out.close();
}
public static void main(String[] args) throws Exception {
readProperties("org/meter/demo/properties/jdbc.properties");
writeProperties("org/meter/demo/properties/output.txt","test2","value2");
writeProperties("org/meter/demo/properties/output.txt","tesdes2","value");
}
}

java读写Properties属性文件公用方法的更多相关文章

  1. 使用JAVA读写Properties属性文件

     使用JAVA读写Properties属性文件 Properties属性文件在JAVA应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数 ...

  2. 解决读写properties属性文件

    package com.kzkj.wx.utils; import java.io.BufferedReader; import java.io.File; import java.io.FileIn ...

  3. java 读写properties (配置)文件

    Properties属性文件在Java应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数据,没有必要使用数据库文件来保存,而使用一般的文 ...

  4. java下properties属性文件操作

    package cn.stat.p1.file; import java.io.File; import java.io.FileInputStream; import java.io.FileNot ...

  5. Code片段 : .properties属性文件操作工具类 & JSON工具类

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “贵专” — 泥瓦匠 一.java.util.Properties API & 案例 j ...

  6. Java学习笔记——JDBC读取properties属性文件

    Java 中的 properties 文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件. 文件的内容是格式是"键=值"(key-valu ...

  7. java:Properties属性文件概念

    java:Properties属性文件概念 在java之前的国际化程序中提出了一个属性文件的概念,属性文件的后缀是:*.properties,那么在java中提供了意个属性文件的专门操作类,Prope ...

  8. Java - 得到项目中properties属性文件中定义的属性值

    public static String getPropertiesValue(String fileName, String key) {   return ResourceBundle.getBu ...

  9. Java读取利用java.util类Properties读取resource下的properties属性文件

    说明:upload.properties属性文件在resources下 import java.io.IOException;import java.io.InputStream;import jav ...

随机推荐

  1. idea Error:java: Compilation failed: internal java compiler error

    idea 遇到Error:java: Compilation failed: internal java compiler error 是提示说你当前使用的编译器jdk版本不对. 按住Ctrl+Alt ...

  2. iOS之中途修改类名

    中途修改类名 在开发过程中,可能感觉之前设置的类名不够代表性,或者发现名字拼写错误等等诸如此类的原因,需要修改类名,不懂技巧的话简直不敢改,现在这里就记录下该类名的方法. 具体步骤如下: .h文件里选 ...

  3. Centos挂载第二块硬盘

    作为一个初创小公司的架构师,工作内容纷繁复杂,涉及了系统管理员.数据库管理员.架构师.高级软件工程师.项目经理的部分.   今天的任务是安装公司的服务器,使用centos6.7.安装过程就不用细讲了. ...

  4. SQL2005 表分区亲测

    --增加文件组 alter database Test add filegroup [FG1] go alter database Test add filegroup [FG2] GO alter ...

  5. BitHacks

    备份文件时看到的.我以前居然下过这东西. 2016-12-4 12:05:52更新 纯文本格式真棒.假如使用word写的我能拷过来格式还不乱?? Markdown真好. Bit Hacks By Se ...

  6. Swift -运算符和循环结构

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #4dbf56 } p.p2 { margin: 0.0px 0. ...

  7. activemq 控制面板里的 Number Of Pending Messages、 Messages Enqueued、Messages Dequeued含义

    Number Of Consumers  消费者 这个是消费者端的消费者数量 Number Of Pending Messages 等待消费的消息 这个是当前未出队列的数量.可以理解为总接收数-总出队 ...

  8. apt28组织新的flash漏洞利用包dealerschoice分析

    17号paloalto发布了文章dealerschoice-sofacys-flash-player-exploit-platform,文中提到apt28正在编写adobe flash player的 ...

  9. js的一些笔记

    var txt = "Hello World!";document.write("<p>字体变大: " + txt.big() + "&l ...

  10. Shell_1 简介

    1 Shell 变量 只读变量 使用 readonly 命令可以将变量定义为只读变量,只读变量的值不能被改变. #!/bin/bash -x varName="AAA" echo ...