随笔都是学习笔记
随笔仅供参考,为避免笔记中可能出现的错误误导他人,请勿转载。
posts - 398,comments - 0,views - 13万

简介:

在国际化程序的资源文件(*.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   时间完全不够用啊  阅读(118)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示