java中经常用到的读写配置文件的信息properties属性的写入,读取例子
package com.util;
import java.io.*;
import java.util.*;
/**
*
* @author zhaizhanpo
*
*
*/
import java.io.*;
import java.util.*;
/**
*
* @author zhaizhanpo
*
*
*/
public class ReadWriteProUtil {
/**
* 根据key读取value
* 在工程中取得相对路径的方法 :this.getClass().getResource("工程路径").getPath();
* @param filePath
* @param key
* @return
*/
public static String readValue(String filePath, String key){
Properties props = new Properties();
FileInputStream in=null;
try{
in = new FileInputStream(filePath);
props.load(in);
String value = props.getProperty(key);
return value;
}catch(Exception e){
e.printStackTrace();
return null;
}finally{
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 读取properties的全部信息
* @param filePath 文件的路径
*/
public static void readProperties(String filePath){
Properties props = new Properties();
InputStream in=null;
try{
in = new BufferedInputStream(new FileInputStream(filePath));
props.load(in);
Enumeration en = props.propertyNames();
while(en.hasMoreElements()){
String key = (String)en.nextElement();
String property = props.getProperty(key);
System.out.println(key + " : " + property);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 写入properties信息
* @param filePath 要写入的文件的路径
* @param parameterName 要改变的值得件
* @param parameterValue 要改成的内容
*/
public static void writeProperties(String filePath, String parameterName, String parameterValue){
Properties props = new Properties();
OutputStream fos=null;
InputStream fis =null;
try{
fis = new FileInputStream(filePath);
//从输入流中读取属性列表(键和元素对)
props.load(fis);
fis.close();
fos = new FileOutputStream(filePath);
props.setProperty(parameterName, parameterValue);
//以适合使用load方法加载到Properties表中的格式,将此Properties表中的属性列表(键和元素对)写入输出流
/**
* 根据key读取value
* 在工程中取得相对路径的方法 :this.getClass().getResource("工程路径").getPath();
* @param filePath
* @param key
* @return
*/
public static String readValue(String filePath, String key){
Properties props = new Properties();
FileInputStream in=null;
try{
in = new FileInputStream(filePath);
props.load(in);
String value = props.getProperty(key);
return value;
}catch(Exception e){
e.printStackTrace();
return null;
}finally{
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 读取properties的全部信息
* @param filePath 文件的路径
*/
public static void readProperties(String filePath){
Properties props = new Properties();
InputStream in=null;
try{
in = new BufferedInputStream(new FileInputStream(filePath));
props.load(in);
Enumeration en = props.propertyNames();
while(en.hasMoreElements()){
String key = (String)en.nextElement();
String property = props.getProperty(key);
System.out.println(key + " : " + property);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 写入properties信息
* @param filePath 要写入的文件的路径
* @param parameterName 要改变的值得件
* @param parameterValue 要改成的内容
*/
public static void writeProperties(String filePath, String parameterName, String parameterValue){
Properties props = new Properties();
OutputStream fos=null;
InputStream fis =null;
try{
fis = new FileInputStream(filePath);
//从输入流中读取属性列表(键和元素对)
props.load(fis);
fis.close();
fos = new FileOutputStream(filePath);
props.setProperty(parameterName, parameterValue);
//以适合使用load方法加载到Properties表中的格式,将此Properties表中的属性列表(键和元素对)写入输出流
//加载额外的内容
String otherContent="making people zhaizhanpo";
props.store(fos,otherContent);
}catch(IOException e){
e.printStackTrace();
}finally{
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
String otherContent="making people zhaizhanpo";
props.store(fos,otherContent);
}catch(IOException e){
e.printStackTrace();
}finally{
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// example 更改用户名称
String filepath="D://Test//src//pro.txt";
ReadWriteProUtil.writeProperties(filepath,"userName","000");
//输出更改后的信息。
System.out.println(ReadWriteProUtil.readValue(filepath,"userName"));
}
}
// example 更改用户名称
String filepath="D://Test//src//pro.txt";
ReadWriteProUtil.writeProperties(filepath,"userName","000");
//输出更改后的信息。
System.out.println(ReadWriteProUtil.readValue(filepath,"userName"));
}
}
***********************************************************************************
注:当前项目路径是
String filepath=System.getProperty("user.dir");
对下面的程序很有用...
/**
* 实现对Java配置文件Properties的读取、写入与更新操作
*/
package test;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
/**
* @author
* @version 2008.11.14
*/
public class SetSystemProperty {
//属性文件的路径
static String profilepath="mail.properties";
/**
* 采用静态方法
*/
private static Properties props = new Properties();
static {
try {
props.load(new FileInputStream(profilepath));
} catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(-1);
} catch (IOException e) {
System.exit(-1);
}
}
/**
* 读取属性文件中相应键的值
* @param key
* 主键
* @return String
*/
public static String getKeyValue(String key) {
return props.getProperty(key);
}
/**
* 根据主键key读取主键的值value
* @param filePath 属性文件路径
* @param key 键名
*/
public static String readValue(String filePath, String key) {
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream(new FileInputStream(
filePath));
props.load(in);
String value = props.getProperty(key);
System.out.println(key +"键的值是:"+ value);
return value;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 更新(或插入)一对properties信息(主键及其键值)
* 如果该主键已经存在,更新该主键的值;
* 如果该主键不存在,则插件一对键值。
* @param keyname 键名
* @param keyvalue 键值
*/
public static void writeProperties(String keyname,String keyvalue) {
try {
// 调用 Hashtable 的方法 put,使用 getProperty 方法提供并行性。
// 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
OutputStream fos = new FileOutputStream(profilepath);
props.setProperty(keyname, keyvalue);
// 以适合使用 load 方法加载到 Properties 表中的格式,
// 将此 Properties 表中的属性列表(键和元素对)写入输出流
props.store(fos, "Update '" + keyname + "' value");
} catch (IOException e) {
System.err.println("属性文件更新错误");
}
}
/**
* 更新properties文件的键值对
* 如果该主键已经存在,更新该主键的值;
* 如果该主键不存在,则插件一对键值。
* @param keyname 键名
* @param keyvalue 键值
*/
public void updateProperties(String keyname,String keyvalue) {
try {
props.load(new FileInputStream(profilepath));
// 调用 Hashtable 的方法 put,使用 getProperty 方法提供并行性。
// 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
OutputStream fos = new FileOutputStream(profilepath);
props.setProperty(keyname, keyvalue);
// 以适合使用 load 方法加载到 Properties 表中的格式,
// 将此 Properties 表中的属性列表(键和元素对)写入输出流
props.store(fos, "Update '" + keyname + "' value");
} catch (IOException e) {
System.err.println("属性文件更新错误");
}
}
//测试代码
public static void main(String[] args) {
readValue("mail.properties", "MAIL_SERVER_PASSWORD");
writeProperties("MAIL_SERVER_INCOMING", "327@qq.com");
System.out.println("操作完成");
}
}
* 实现对Java配置文件Properties的读取、写入与更新操作
*/
package test;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
/**
* @author
* @version 2008.11.14
*/
public class SetSystemProperty {
//属性文件的路径
static String profilepath="mail.properties";
/**
* 采用静态方法
*/
private static Properties props = new Properties();
static {
try {
props.load(new FileInputStream(profilepath));
} catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(-1);
} catch (IOException e) {
System.exit(-1);
}
}
/**
* 读取属性文件中相应键的值
* @param key
* 主键
* @return String
*/
public static String getKeyValue(String key) {
return props.getProperty(key);
}
/**
* 根据主键key读取主键的值value
* @param filePath 属性文件路径
* @param key 键名
*/
public static String readValue(String filePath, String key) {
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream(new FileInputStream(
filePath));
props.load(in);
String value = props.getProperty(key);
System.out.println(key +"键的值是:"+ value);
return value;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 更新(或插入)一对properties信息(主键及其键值)
* 如果该主键已经存在,更新该主键的值;
* 如果该主键不存在,则插件一对键值。
* @param keyname 键名
* @param keyvalue 键值
*/
public static void writeProperties(String keyname,String keyvalue) {
try {
// 调用 Hashtable 的方法 put,使用 getProperty 方法提供并行性。
// 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
OutputStream fos = new FileOutputStream(profilepath);
props.setProperty(keyname, keyvalue);
// 以适合使用 load 方法加载到 Properties 表中的格式,
// 将此 Properties 表中的属性列表(键和元素对)写入输出流
props.store(fos, "Update '" + keyname + "' value");
} catch (IOException e) {
System.err.println("属性文件更新错误");
}
}
/**
* 更新properties文件的键值对
* 如果该主键已经存在,更新该主键的值;
* 如果该主键不存在,则插件一对键值。
* @param keyname 键名
* @param keyvalue 键值
*/
public void updateProperties(String keyname,String keyvalue) {
try {
props.load(new FileInputStream(profilepath));
// 调用 Hashtable 的方法 put,使用 getProperty 方法提供并行性。
// 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
OutputStream fos = new FileOutputStream(profilepath);
props.setProperty(keyname, keyvalue);
// 以适合使用 load 方法加载到 Properties 表中的格式,
// 将此 Properties 表中的属性列表(键和元素对)写入输出流
props.store(fos, "Update '" + keyname + "' value");
} catch (IOException e) {
System.err.println("属性文件更新错误");
}
}
//测试代码
public static void main(String[] args) {
readValue("mail.properties", "MAIL_SERVER_PASSWORD");
writeProperties("MAIL_SERVER_INCOMING", "327@qq.com");
System.out.println("操作完成");
}
}
转载于:https://blog.51cto.com/maliujiang/821873