Java读写Properties
来源:互联网 发布:为什么谭咏麟你知我知 编辑:程序博客网 时间:2024/06/06 10:39
Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件。
3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。
4. store ( OutputStream out, String comments),以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
注释信息后面是属性文件的当前保存时间信息。(一直存在)
5. clear (),清除所有装载的 键 - 值对。该方法在基类中提供。
在Java中,其配置文件常为以properties结尾的文件,格式为文本文件,文件的内容的格式是“键=值”的格式,文本注释信息可以用"#"来注释。
class Properties extends Hashtable<Object,Object>
Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集。
不过Properties有特殊的地方,就是它的键和值都是字符串类型。
1. getProperty ( String key),用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。
FileInputStream in = new FileInputStream("test.properties");properties.load(in);properties.list(System.out);in.close();
3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。
4. store ( OutputStream out, String comments),以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
FileOutputStream out = new FileOutputStream("test.properties");properties.store(out, "commit");out.close();如果comments不为空,保存后的属性文件第一行会是#comments,表示注释信息;如果为空null则没有注释信息。
注释信息后面是属性文件的当前保存时间信息。(一直存在)
5. clear (),清除所有装载的 键 - 值对。该方法在基类中提供。
举个栗子:
获取JVM系统配置:
Properties properties = System.getProperties();properties.list(System.out);
详细的一个栗子:
package com.xiya.test;import java.io.*;import java.util.Enumeration;import java.util.Properties;/** * Created by N3verL4nd on 2017/4/11. */public class Main { public static void main(String[] args) { //Properties properties = System.getProperties(); //properties.list(System.out); Properties properties = new Properties(); properties.put("user", "root"); properties.put("password", "root"); properties.put("characterEncoding", "UTF-8"); properties.put("useSSL", "true"); properties.put("useUnicode", "true"); OutputStream out = null; try { out = new FileOutputStream("test.properties"); properties.store(out, "commit"); } catch (IOException e) { e.printStackTrace(); } finally { if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } InputStream in = null; try { in = new BufferedInputStream(new FileInputStream("test.properties")); properties.load(in); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } properties.list(System.out); Enumeration<?> names = properties.propertyNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); System.out.println(name + ":" + properties.get(name)); } }}
#commit#Tue Apr 11 14:45:01 CST 2017useUnicode=trueuser=rootpassword=rootcharacterEncoding=UTF-8useSSL=true
http://www.tutorialspoint.com/java/java_properties_class.htm
http://www.journaldev.com/712/java-properties-file-java-util-properties
http://www.mkyong.com/java/java-properties-file-examples/
Java读取Properties文件的六种方法
http://blog.csdn.net/Senton/article/details/4083127
0 0
- Java读写Properties文件
- java读写properties文件
- java读写properties文件
- Java读写properties文件
- Java读写Properties文件
- Java读写Properties文件
- java读写 properties
- Java读写properties文件
- Java 读写Properties配置文件
- Java 读写Properties配置文件
- Java读写properties配置文件
- Java读写Properties文件
- Java读写Properties文件
- java读写properties
- java读写properties配置文件
- Java 读写Properties配置文件
- Java读写Properties配置文件
- Java 读写Properties配置文件
- 14. Longest Common Prefix
- 递归思想
- LeetCode 190 Reverse Bits
- 如何使用PictureBox播放视频
- Evaluate Division
- Java读写Properties
- 数据库基础知识_2
- Qt 连接MySQL 驱动问题
- 类序列化问题-java.io.InvalidClassException
- 1015. Reversible Primes (20)
- 仿微信设置字体大小控件
- gcc编译时对’xxxx’未定义的引用问题
- ios学习第五天(三)View常用操作
- 三步学会Java Socket编程