随笔都是学习笔记
随笔仅供参考,为避免笔记中可能出现的错误误导他人,请勿转载。

简介:

在国际化程序的资源文件(*.properties)中,数据是按照 Key=Value 的格式存储的,而这种结构的保存形式和Map集合很相似,但是唯一的区别在于其所保存的内容只能够是字符串,那么为了方便地描述属性的定义,在java.util包里面提供有一个Properties类型,此类是Hashtable的子类;

public class Properties 
extends Hashtable<Object,Object>

可以发现继承Hashtable的泛型定义是两个Object类型,Properties不能操作泛型,只能操作String类型。

NO.

方法名称

类型

功能

1
public synchronized Object setProperty(String key, String value) {
        return put(key, value);
    }
普通 设置属性
2
public String getProperty(String key) {
        Object oval = map.get(key);
        String sval = (oval instanceof String) ? (String)oval : null;
        Properties defaults;
        return ((sval == null) && ((defaults = this.defaults) != null)) ? defaults.getProperty(key) : sval;
    }
普通 获取属性,不存在返回null
3
public String getProperty(String key, String defaultValue) {
        String val = getProperty(key);
        return (val == null) ? defaultValue : val;
    }
普通 获取属性,不存在返回默认值
4
public void store(OutputStream out, String comments)
        throws IOException
    {
        store0(new BufferedWriter(new OutputStreamWriter(out, ISO_8859_1.INSTANCE)),
               comments,
               true);
    }
普通 输出属性内容
5
public void load(Properties props, InputStream in)
        throws IOException, InvalidPropertiesFormatException, UnsupportedEncodingException
    {
        this.properties = props;

        try {
            SAXParser parser = new SAXParserImpl();
            parser.parse(in, this);
        } catch (SAXException saxe) {
            throw new InvalidPropertiesFormatException(saxe);
        }

        /**
         * String xmlVersion = propertiesElement.getAttribute("version"); if
         * (xmlVersion.compareTo(EXTERNAL_XML_VERSION) > 0) throw new
         * InvalidPropertiesFormatException( "Exported Properties file format
         * version " + xmlVersion + " is not supported. This java installation
         * can read" + " versions " + EXTERNAL_XML_VERSION + " or older. You" +
         * " may need to install a newer version of JDK.");
         */
    }
普通 通过输入流读取属性内容

代码实现:

import java.util.Hashtable;
import java.util.Properties;

public class MAIN {
    public static void main(String[] args) {
        Properties pop = new Properties();
        // 设置只能是字符串
        pop.setProperty("one", "1");
        pop.setProperty("on", "2");
        pop.setProperty("o", "3");
        System.out.println("通过Key查看存在的Value: " + pop.getProperty("o"));        // 通过Key查看存在的Value
        System.out.println("通过Key查看存在的Value: " + pop.getProperty("on"));        // 通过Key查看存在的Value
        System.out.println("通过Key查看存在的Value: " + pop.getProperty("one"));        // 通过Key查看存在的Value
        System.out.println("通过Key查看不存在的Value: " + pop.getProperty("e"));        // 通过Key查看不存在的Value
        System.out.println("设置默认值再通过Key查看不存在的Value: " + pop.getProperty("e","Defualt"));        // 设置默认值再通过Key查看不存在的Value
    }

}

输出结果:

可以发现Properties里面可以想Map集合那样进行Key=Value的查询,但是唯一的区别就是只能够操作String类型

提供Properties的一个重要原因是:它可以通过输出流输出属性,也可以通过输入流读取属性内容,这是Map集合没有的;

内容写入操作:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Hashtable;
import java.util.Properties;

public class MAIN {
    public static void main(String[] args) throws Exception{
        Properties pop = new Properties();
        // 设置只能是字符串
        pop.setProperty("one", "1");
        pop.setProperty("on", "2");
        pop.setProperty("o", "3");
        pop.store(new FileOutputStream(new File("D:"+ File.separator + "Info.txt")), "中文 - English");
    }
}

输出结果:

文本中的中文内容会自动进行转码;

 

读取操作:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Hashtable;
import java.util.Properties;

public class MAIN {
    public static void main(String[] args) throws Exception{
        Properties pop = new Properties();
        pop.load(new FileInputStream(new File("D:"+ File.separator + "Info.txt")));
        System.out.println(pop.getProperty("one"));
        
    }
}

输出结果

使用Properties最大的特点就是:进行资源内容的输入与输出操作;

实际情况中Properties一般用于读取配置资源文件的信息,主要是在程序标准设计的初始化准备的时候使用。

 

 

Collections工具 【JDK1.2】

Collections是Java提供的一组集合数据的操作工具类,利用它可以进行各个集合的操作。

 

public class Collections

 

操作关系:

从上面的操作关系可以发现,Collections类就是专门为集合进行各种数据操作而存在的工具类,使集合的数据操作更加便利,比如二分查找,如果单独实现是非常麻烦的,但是在Collections中提供有专门的方法,就只需要调用就行。

 

代码实现:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MAIN {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        Collections.addAll(list,"Hello","World","China");    // 数据的添加
        Collections.reverse(list);    // 元素反转
        System.out.println(list);
        System.out.println("World:" + Collections.binarySearch(list, "World"));    // 二分查找
    }
}

输出结果:

 

 

 

面试题:

请解释Collection 与 Collections 的区别?.

 

  - Collection是集合接口,允许保存单值对象;

  - Collections是集合操作的工具类。

 

 

 

 

posted on 2022-03-19 13:06  时间完全不够用啊  阅读(97)  评论(0编辑  收藏  举报