美文网首页
Enumeration接口,StringTokenizer,Ha

Enumeration接口,StringTokenizer,Ha

作者: VikingOldYoung | 来源:发表于2016-11-02 22:35 被阅读0次

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"));

相关文章

  • Enumeration接口,StringTokenizer,Ha

    Enumeration接口 该接口较为古老,但在维护以前的程序时就会频繁遇到。枚举Enumeration接口,作用...

  • Enumeration接口

    在学习properties类的过程使用到Enumeration接口,因此学习记录下。 Enumeration接口中...

  • Enumeration与Iterator介绍

    Enumeration Enumeration简介 Enumeration(列举),本身是一个接口,不是一个类。E...

  • Enumeration 接口浅析

    注:基于 jdk 1.8 版本。 一、Enumeration 是什么? Enumeration 不是一个数据结构,...

  • Java Enumeration接口

    import java.util.Vector;import java.util.Enumeration; /* ...

  • Java 数据结构

    一.枚举(Enumeration) 枚举(Enumeration)接口虽然它本身不属于数据结构,但它在其他数据结构...

  • Enumeration 接口使用方法

    1.自定义数据结构 实现Enumeration接口 2.数据类 [需要提供一个方法,返回一个Enumerati...

  • java数据结构

    一、枚举Enumeration(比较传统的接口,已被迭代器取代) 枚举接口定义了一种从数据结构中取回连续元素的方式...

  • Enumerations

    Enumeration Syntax The name of enumeration starts with a ...

  • Java 枚举

    Enumeration接口中定义了一些方法,通过这些方法可以枚举(一次获得一个)对象集合中的元素。这种传统接口已被...

网友评论

      本文标题:Enumeration接口,StringTokenizer,Ha

      本文链接:https://www.haomeiwen.com/subject/mphbuttx.html