1. StringTokenizer:
字符串分割类:
1public class TestALL { 2 public static void main(String[] args) { 3 System.out.println("默认以空格,\\t,\\r,\\n分割"); 4 StringTokenizer st = new StringTokenizer("www ooobj com"); 5 while(st.hasMoreElements()){ 6 System.out.println("Token:" + st.nextToken()); 7 } 8 System.out.println("指定以.分割"); 9 st = new StringTokenizer("www.ooobj.com","."); 10 while(st.hasMoreElements()){ 11 System.out.println("Token:" + st.nextToken()); 12 } 13 System.out.println("指定以.分割,并在结果中包含分隔符"); 14 st = new StringTokenizer("www.ooobj.com",".",true); 15 while(st.hasMoreElements()){ 16 System.out.println("Token:" + st.nextToken()); 17 } 18 } 19}
输出:
1默认以空格,\t,\r,\n分割 2Token:www 3Token:ooobj 4Token:com 5指定以.分割 6Token:www 7Token:ooobj 8Token:com 9指定以.分割,并在结果中包含分隔符 10Token:www 11Token:. 12Token:ooobj 13Token:. 14Token:com
2. DateFormat:
想必大家对SimpleDateFormat并不陌生。SimpleDateFormat 是 Java 中一个非常常用的类,该类用来对日期字符串进行解析和格式化输出,但如果使用不小心会导致非常微妙和难以调试的问题,因为 DateFormat 和 SimpleDateFormat 类不都是线程安全的,在多线程环境下调用 format() 和 parse() 方法应该使用同步代码来避免问题。
最好的方法,使用ThreadLocal:
1package com.peidasoft.dateformat; 2 3import java.text.DateFormat; 4import java.text.ParseException; 5import java.text.SimpleDateFormat; 6import java.util.Date; 7 8public class ConcurrentDateUtil { 9 10 private static ThreadLocal<DateFormat> threadLocal = new ThreadLocal<DateFormat>() { 11 @Override 12 protected DateFormat initialValue() { 13 return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 14 } 15 }; 16 17 public static Date parse(String dateStr) throws ParseException { 18 return threadLocal.get().parse(dateStr); 19 } 20 21 public static String format(Date date) { 22 return threadLocal.get().format(date); 23 } 24}
3. Java获取路径:
1public String getCurrentPath(){ 2 //取得根目录路径 3 String rootPath=getClass().getResource("/").getFile().toString(); 4 //当前目录路径 5 String currentPath1=getClass().getResource(".").getFile().toString(); 6 String currentPath2=getClass().getResource("").getFile().toString(); 7 //当前目录的上级目录路径 8 String parentPath=getClass().getResource("../").getFile().toString(); 9 10 return rootPath; 11 12 }
4. 线程安全结合类:
Collection 是对象集合, Collection 有两个子接口 List 和 Set
List 可以通过下标 (1,2..) 来取得值,值可以重复,而 Set 只能通过游标来取值,并且值是不能重复的
ArrayList , Vector , LinkedList 是 List 的实现类
ArrayList 是线程不安全的, Vector 是线程安全的,这两个类底层都是由数组实现的
LinkedList 是线程不安全的,底层是由链表实现的
Map 是键值对集合
HashTable 和 HashMap 是 Map 的实现类
HashTable 是线程安全的,不能存储 null 值
HashMap 不是线程安全的,可以存储 null 值