Java操作属性文件
2013-12-30 22:01
387 查看
1、读取属性文件
2、写入属性文件
/** * 读取属性值 * @param filePath * @param key * @param defValue * @return */ public String read(String filePath, String key, String defValue) { Properties props = new Properties(); try { // 如果文件不存在,返回默认值 File file = new File(filePath); if (!file.exists()) { return defValue; } InputStream fis = new FileInputStream(filePath); props.load(fis); fis.close(); return props.getProperty(key) == null ? defValue : props.getProperty(key); } catch (IOException e) { System.out.println("读取文件出错了:" + e); return defValue; } }
2、写入属性文件
/** * 写入属性值 * @param filePath * @param key * @param value */ public boolean write(String filePath, String key, String value) { Properties props = new Properties(); try { // 如果文件不存在,创建一个新的 File file = new File(filePath); if (!file.exists()) { file.createNewFile(); } InputStream fis = new FileInputStream(filePath); props.load(fis); fis.close(); // 关闭流 OutputStream fos = new FileOutputStream(filePath); props.setProperty(key, value); props.store(fos, key); fos.close(); // 关闭流 return true; } catch (FileNotFoundException e) { System.out.println("文件不存在!"); } catch (IOException e) { System.out.println("写入文件出错了:" + e); } return false; }
相关文章推荐
- JAVA操作属性文件
- Java操作属性文件,支持新增或更新多个属性
- Java本地文件操作(四)文件属性的设置
- java操作属性文件工具类
- java操作属性文件
- java中properties属性文件的读写修改操作
- Java操作属性文件,支持新增或更新多个属性
- java 属性文件的读取操作
- poi读取文件,并获取相应属性 (java操作Excel文件)
- [java]PropertiesUtils操作属性文件
- java中对于文件属性的一些操作
- java操作.properties属性文件
- JAVA操作属性文件,可进行读、写、更改
- JAVA操作属性文件,可进行读、写、更改
- Java属性文件的操作
- Java操作属性文件之工具类
- (转)java中对于文件属性的一些操作
- HDFS java操作(二)FileStatus 获取文件属性,globStatus 进行路径过滤
- java文件属性操作File类
- java操作属性文件