java读写自定义property文件
来源:互联网 发布:燕郊招聘网络推广专员 编辑:程序博客网 时间:2024/05/20 14:15
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"); }*/}
0 0
- java读写自定义property文件
- java读写property文件
- Java读写修改Property文件
- Java读写修改Property文件
- Java读写修改Property文件
- Java读写修改Property文件
- Java读写Property
- 自定义文件的读写
- 如何读写property文件,保证一定顺序
- java 读取property文件
- java 读取property文件
- java读取property文件
- Java解析Property文件
- Java文件操作property文件
- Java property文件中文解决方案
- 关于java的property文件
- Java property文件中文解决方案
- Java读取操作Property文件
- Udp DDOS攻击代码实现
- 开启梦幻般的webrtc之旅
- 揭开Wayland的面纱(二):Wayland应运而生
- MPAndroidChart 3.0——LineChart(折线图)
- 【个人笔记重点,不作为参考】主题:ubuntu 14.04 nfs安装与配置
- java读写自定义property文件
- opencv使用小技巧
- ssh登录报ssh_exchange_identification: read: Connection reset by peer
- 找最小的K个数
- 从零开始的JAVA学习
- C++11的新特性
- 解决Eclipse中TestNG乱码问题
- Redis缓存命令整理—String字符串操作命令
- 割点_去掉割点后的连通分支数(点连通图)