Enumeration接口,StringTokenizer,Hashtable,Porperties

Enumeration接口

该接口较为古老,但在维护以前的程序时就会频繁遇到。
枚举Enumeration接口,作用和Iterator类似,都是遍历数据用到的。
方法

  1. hasMorElements();
  2. nextElements();

使用示例

public class Demo01 {

    public static void main(String[] args) {
        Vector<String> vector=new Vector<>();
        vector.add("Viking");
        vector.add("Tome");
        vector.add("Gailun");
        vector.add("Forint");
        
        Enumeration<String> e=vector.elements();
        while(e.hasMoreElements()){
            String temp=e.nextElement();
            System.out.println(temp);
        }
    }
}

StringTokenizer类

它实现了Enumeration接口,可用于字符串分割。
与String 类的spilt();作用相似,但它不支持正则表达式

public class Demo02 {

    public static void main(String[] args) {
        
        String str="hello world;you are my sunshine;please call it later";
        StringTokenizer token=new StringTokenizer(str, ";");
        //因为StringTokenizer类实现了Enumeration接口
        while(token.hasMoreElements()){
            System.out.println(token.nextElement());;
        }
    }
}

Hashtable

Map的实现类,与HashMap操作相同
区别:
Hashtable 线程安全,键与值都不能为null,父类为Dictionary
HashMap 线程不安全,键和值可以为null,父类为AbstractMap
不细说了,没啥难度。

Porperties

继承自Hashtable

  1. 作用:读写资源配置文件
  2. 键与值只能为字符串
  3. 方法
  • 普通方法:
    setProperty(String key,String value);
    getProperty(String key);该方法如果不存在则返回null
    getProperty(String key,String defaultValue) 如果不存在则返回默认值
    不推荐使用put,get方法
public class Demo03 {

    public static void main(String[] args) {

        Properties p=new Properties();
        p.put("a", "Viking");
        p.put("b", "Tome");
        p.put("c", "Mark");
        
        System.out.println(p.getProperty("a"));
        System.out.println(p.getProperty("d"));
        System.out.println(p.getProperty("d","test"));
        }
}
结果为
Viking
null
test
  • 存储与读取什么流的方法
    后缀为.properties
    stroe(OutputStream out,String comments);
    store(Writer writer,String comments);
    load(InputStream inStream);
    load(Reader reader);
    后缀为.xml
    storeToXML(OutputStream out,String comments); UTF-8字符集
    storeToXML(OutputStream out,String comments,String encoding);
    自定义字符集
    loadFromXML(inputStream in);
public class Demo03 {
    
    public static void main(String[] args) throws FileNotFoundException, IOException {
    
        Properties p=new Properties();
        p.put("a", "Viking");
        p.put("b", "Tome");
        p.put("c", "Mark");
        //使用绝对路径,会保存在计算机的相应位置
        p.store(new FileOutputStream(new File("/home/viking/文档/db.properties")), "db");
        p.storeToXML(new FileOutputStream(new File("/home/viking/文档/db.xml")), "db");
        //使用相对路径,默认为当前项目工程
        p.store(new FileOutputStream(new File("db.properties")), "db");
    }
}
  1. 类路径加载资源文件
    类所在的根路径 bin
    由于项目提交时只会提交class文件,即bin文件夹,java源码是不会提交的。所以需要根据类路径加载配置资源文件
  • 类.class.getResourceAsStream("/");//'/'表示bin
  • Thread.currentThread().getContextClassLoader().getResourceAsStream(""); //空表示bin

以上都默认到bin文件夹

p1.load(Demo03.class.getResourceAsStream("/test/db.properties"));
p1.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("test/db.properties"));
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,131评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,805评论 18 399
  • JAVA面试题 1、作用域public,private,protected,以及不写时的区别答:区别如下:作用域 ...
    JA尐白阅读 1,193评论 1 0
  • 问这个问题的人都该打,先拖出去,赏一丈红~ 如果你是计算机学生或者意欲从事IT行业的人,那么再仗50大板!! 为什...
    小柑阅读 80,193评论 14 82
  • 我以前住的地方,只有一个窄小的阳台。另一半隔成了厨房。阳台上放了一台房东的洗衣机,还有一把漆成绿色的木梯。我在阳台...
    f66f898d9575阅读 409评论 0 0