2、特点
properties被称为属性类对象 properties类表示了一个持久的属性集 properties是一个和IO流向结合的集合 properties的key和value都是String类型 3、构造方法 public Properties() { this(null); } public Properties(Properties defaults) { this.defaults = defaults; } 4、常用方法 //load方法 public synchronized void load(Reader reader) throws IOException { load0(new LineReader(reader)); } public synchronized void load(InputStream inStream) throws IOException { load0(new LineReader(inStream)); } //store方法 public void store(Writer writer, String comments) throws IOException { store0((writer instanceof BufferedWriter)?(BufferedWriter)writer : new BufferedWriter(writer), comments, false); } public void store(OutputStream out, String comments) throws IOException { store0(new BufferedWriter(new OutputStreamWriter(out, "8859_1")), comments, true); } //get方法 public String getProperty(String key) { Object oval = super.get(key); String sval = (oval instanceof String) ? (String)oval : null; return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval; } public String getProperty(String key, String defaultValue) { String val = getProperty(key); return (val == null) ? defaultValue : val; } //set方法 public synchronized Object setProperty(String key, String value) { return put(key, value); }2、get方法
获取集合中的元素,这里调用了get方法,这个方法是Hashtable的get方法。 //get方法 public String getProperty(String key) { Object oval = super.get(key); String sval = (oval instanceof String) ? (String)oval : null; return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval; } public String getProperty(String key, String defaultValue) { String val = getProperty(key); return (val == null) ? defaultValue : val; } public synchronized V get(Object key) { Entry<?,?> tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) { if ((e.hash == hash) && e.key.equals(key)) { return (V)e.value; } } return null; }3、load方法
将流中的数据添加到集合中,其实就是将硬盘中保存的文件(键值对),读取到集合中使用。 void load(InputStream inStream) void load(Reader reader)