java读写自定义property文件

时间:2021-10-16 22:17:40
import java.io.*;
import java.net.URI;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;

public class PropertiesUtils {
private static Properties properties;
private URI uri;

/**
* 加载属性文件
*
* @param filePath 文件路径
* @return
*/
public static Properties loadProps(String filePath) {
properties = new Properties();
String path = null;
try {
URL xmlpath = PropertiesUtils.class.getClassLoader().getResource(filePath);
if (xmlpath != null) {
path = xmlpath.getPath();
// InputStream in = new BufferedInputStream(new FileInputStream(path));
InputStream in = PropertiesUtils.class.getClassLoader().getResourceAsStream(filePath);
properties.load(in);
}
} catch (Exception e) {
e.printStackTrace();
}
return properties;
}

/**
* 读取配置文件
*
* @param
* @param key
* @return
*/
public static String getString(Properties properties, String key) {
return properties.getProperty(key);
}

/**
* 更新配置文件
*
* @return
*/
public static void updatePropertyLoginError(Properties properties, String filePath, String keyname, String keyvalue) {
try {
properties.setProperty(keyname, keyvalue);
String path = PropertiesUtils.class.getClassLoader().getResource(filePath).getPath();
FileOutputStream outputFile = new FileOutputStream(path);
properties.store(outputFile, "modify");
outputFile.flush();
outputFile.close();
} catch (Exception e) {
e.printStackTrace();
}

}

/**
*
* @param filePath
* @param key
* @return
*/
public static String GetValueByKey(String filePath, String key) {
Properties pps = new Properties();
try {
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
pps.load(in);
String value = pps.getProperty(key);
System.out.println(key + " = " + value);
return value;

} catch (IOException e) {
e.printStackTrace();
return null;
}
}

//读取Properties的全部信息
/**
*
* @throws IOException
*/
public static void GetAllProperties() throws IOException {
Properties pps = new Properties();
String filePath = null;
URL xmlpath = PropertiesUtils.class.getClassLoader().getResource("raspberry.properties");
System.out.println(xmlpath);
if (xmlpath != null) {
filePath = xmlpath.getPath();
}
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
pps.load(in);
Enumeration en = pps.propertyNames(); //得到配置文件的名字

while (en.hasMoreElements()) {
String strKey = (String) en.nextElement();
String strValue = pps.getProperty(strKey);
System.out.println(strKey + "=" + strValue);
}

}

/**
*
* @param filePath
* @param pKey
* @param pValue
* @return
* @throws IOException
*/
public static boolean WriteProperties(String filePath, String pKey, String pValue) throws IOException {
Properties pps = new Properties();
String path = null;
URL xmlpath = PropertiesUtils.class.getClassLoader().getResource(filePath);

if(null!=xmlpath){
path = xmlpath.getPath();
InputStream in = new FileInputStream(path);
//从输入流中读取属性列表(键和元素对)
pps.load(in);
//调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
//强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
OutputStream out = new FileOutputStream(path);
pps.setProperty(pKey, pValue);
//以适合使用 load 方法加载到 Properties 表中的格式,
//将此 Properties 表中的属性列表(键和元素对)写入输出流
pps.store(out, "Update" + pKey + "name");
out.flush();
out.close();
return true;
}else{
return false;
}

}

/**
*
* @param properties
* @param filePath
* @param keyname
* @param keyvalue
*/
public static void updateProperty(Properties properties,String filePath,String keyname,String keyvalue) {
try {

// 从输入流中读取属性列表(键和元素对)
properties.setProperty(keyname, keyvalue);
FileOutputStream outputFile = new FileOutputStream(filePath);
properties.store(outputFile, null);
outputFile.flush();
outputFile.close();
} catch (Exception e) {
e.printStackTrace();
}
}

/*public static void main(String[] args) throws IOException {

URL xmlpath = PropertiesUtils.class.getClassLoader().getResource("raspberry.properties");
System.out.println(xmlpath);
properties = loadProps(xmlpath.getPath());
// updateProperty(properties,"raspberry.properties","password","wwwwwwww");

// GetAllProperties();
WriteProperties("raspberry.properties","userName","microlink");
}*/


}