熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Apache Commons Lang 包

2013-11-23 19:26:02  來源: Java核心技術 

  Apache Commons Lang 包是用來處理Java基本對象方法的工具類包可以簡化很多平時經常要用到的寫法比如

  SerializationUtils類為序列化工具類也是lang包下的工具主要用於序列化操作同時提供對象克隆接口

  ToStringBuilder類功能就是在自己定義一個類的toString 方法時方便的格式化類的屬性ToStringBuilder類中的append方法可以向該類添加基本類型數組和對象 只有添加的方法才會被toString方法輸出ToStringStyle類則是對輸出的內容進行格式化

  EqualsBuilder與HashCodeBuilder類可以簡化Java類中equals與hashCode方法的改寫過程

  下面是一個使用了各個工具的小例子

   import javaioFile;

    import javaioFileInputStream;

    import javaioFileNotFoundException;

    import javaioFileOutputStream;

    import javaioIOException;

    import javautilCalendar;

    import javautilDate;

    import javautilIterator;

  

    import monslangArrayUtils;

    import monslangCharSet;

    import monslangCharSetUtils;

    import monslangClassUtils;

    import monslangObjectUtils;

    import monslangRandomStringUtils;

    import monslangSerializationUtils;

    import monslangStringEscapeUtils;

    import monslangStringUtils;

    import monslangSystemUtils;

    import monslangValidate;

    import monslangbuilderEqualsBuilder;

    import monslangbuilderHashCodeBuilder;

    import monslangbuilderToStringBuilder;

    import monslangbuilderToStringStyle;

    import monslangmathNumberUtils;

    import monslangtextWordUtils;

    import monslangtimeDateFormatUtils;

    import monslangtimeDateUtils;

    import monslangtimeStopWatch;

  

    public class TestLangDemo {

  

  

  public void charSetDemo() {

  

  Systemoutprintln(**CharSetDemo**

  

  CharSet charSet = CharSetgetInstance(aeiou

  

  String demoStr = The quick brown fox jumps over the lazy dog;

  

  int count = ;

  

  for (int i = len = demoStrlength() i < len; i++) {

  

  if (ntains(demoStrcharAt(i))) {

  

  count++;

  

  }

  

  }

  

  Systemoutprintln(count: + count)

  

  }

  

  

  public void charSetUtilsDemo() {

  

  Systemoutprintln(**CharSetUtilsDemo**

  

  Systemoutprintln(計算字符串中包含某字符數

  

  Systemoutprintln(unt(

  

  The quick brown fox jumps over the lazy dog aeiou))

  

  

  Systemoutprintln(刪除字符串中某字符

  

  Systemoutprintln(CharSetUtilsdelete(

  

  The quick brown fox jumps over the lazy dog aeiou))

  

  

  Systemoutprintln(保留字符串中某字符

  

  Systemoutprintln(CharSetUtilskeep(

  

  The quick brown fox jumps over the lazy dog aeiou))

  

  

  Systemoutprintln(合並重復的字符

  

  Systemoutprintln(CharSetUtilssqueeze(a  bbbbbb

  c dd b d))

  

  }

  

  

  public void objectUtilsDemo() {

  

  Systemoutprintln(**ObjectUtilsDemo**

  

  Systemoutprintln(Object為null時默認打印某字符

  

  Object obj = null;

  

  Systemoutprintln(ObjectUtilsdefaultIfNull(obj ))

  

  

  Systemoutprintln(驗證兩個引用是否指向的Object是否相等取決於Object的equals()方法

  

  Object a = new Object()

  

  Object b = a;

  

  Object c = new Object()

  

  Systemoutprintln(ObjectUtilsequals(a b))

  

  Systemoutprintln(ObjectUtilsequals(a c))

  

  

  Systemoutprintln(用父類Object的toString()方法返回對象信息

  

  Date date = new Date()

  

  Systemoutprintln(ObjectUtilsidentityToString(date))

  

  Systemoutprintln(date)

  

  

  Systemoutprintln(返回類本身的toString()方法結果對象為null時返回長度字符串

  

  Systemoutprintln(ObjectUtilstoString(date))

  

  Systemoutprintln(ObjectUtilstoString(null))

  

  Systemoutprintln(date)

  

  }

  

  

  public void serializationUtilsDemo() {

  

  Systemoutprintln(*SerializationUtils**

  

  Date date = new Date()

  

  byte[] bytes = SerializationUtilsserialize(date)

  

  Systemoutprintln(ArrayUtilstoString(bytes))

  

  Systemoutprintln(date)

  

  

  Date reDate = (Date) SerializationUtilsdeserialize(bytes)

  

  Systemoutprintln(reDate)

  

  Systemoutprintln(ObjectUtilsequals(date reDate))

  

  Systemoutprintln(date == reDate)

  

  

  FileOutputStream fos = null; 

  FileInputStream fis = null; 

  try { 

  fos = new FileOutputStream(new File(d:/testtxt)) 

  fis = new FileInputStream(new File(d:/testtxt)) 

  SerializationUtilsserialize(date fos) 

  Date reDate = (Date) SerializationUtilsdeserialize(fis) 

  

  Systemoutprintln(dateequals(reDate)) 

  

  } catch (FileNotFoundException e) { 

  eprintStackTrace() 

  } finally { 

  try { 

  fosclose() 

  fisclose() 

  } catch (IOException e) { 

  eprintStackTrace() 

  } 

  } 

  

  } 

  

  public void randomStringUtilsDemo() { 

  Systemoutprintln(**RandomStringUtilsDemo** 

  Systemoutprintln(生成指定長度的隨機字符串好像沒什麼用 

  Systemoutprintln(RandomStringUtilsrandom()) 

  

  Systemoutprintln(在指定字符串中生成長度為n的隨機字符串 

  Systemoutprintln(RandomStringUtilsrandom( abcdefghijk)) 

  

  Systemoutprintln(指定從字符或數字中生成隨機字符串 

  Systemoutprintln(RandomStringUtilsrandom( true false)) 

  Systemoutprintln(RandomStringUtilsrandom( false true)) 

  

  } 

  

  public void stringUtilsDemo() { 

  Systemoutprintln(**StringUtilsDemo** 

  Systemoutprintln(將字符串重復n次將文字按某寬度居中將字符串數組用某字符串連接 

  String[] header = new String[]; 

  header[] = StringUtilsrepeat(*  

  header[] = StringUtilscenter(  StringUtilsDemo  ^O^ 

  header[] = header[]; 

  String head = StringUtilsjoin(header \n 

  Systemoutprintln(head) 

  

  Systemoutprintln(縮短到某長度用…結尾 

  Systemoutprintln(StringUtilsabbreviate( 

  The quick brown fox jumps over the lazy dog )) 

  Systemoutprintln(StringUtilsabbreviate( 

  The quick brown fox jumps over the lazy dog )) 

  

  Systemoutprintln(返回兩字符串不同處索引號 

  Systemoutprintln(StringUtilsindexOfDifference(aaabc aaacc)) 

  

  Systemoutprintln(返回兩字符串不同處開始至結束 

  Systemoutprintln(StringUtilsdifference(aaabcde aaaccde)) 

  

  Systemoutprintln(截去字符串為以指定字符串結尾的部分 

  Systemoutprintln(StringUtilschomp(aaabcde de)) 

  

  Systemoutprintln(檢查一字符串是否為另一字符串的子集 

  Systemoutprintln(ntainsOnly(aad aadd)) 

  

  Systemoutprintln(檢查一字符串是否不是另一字符串的子集 

  Systemoutprintln(ntainsNone(defg aadd)) 

  

  Systemoutprintln(檢查一字符串是否包含另一字符串 

  Systemoutprintln(ntains(defg ef)) 

  Systemoutprintln(ntainsOnly(ef defg)) 

  

  Systemoutprintln(返回可以處理null的toString() 

  Systemoutprintln(StringUtilsdefaultString(aaaa)) 

  Systemoutprintln(? + StringUtilsdefaultString(null) + ! 

  

  Systemoutprintln(去除字符中的空格 

  Systemoutprintln(StringUtilsdeleteWhitespace(aa  bb  cc)) 

  

  Systemoutprintln(分隔符處理成數組 

  String[] strArray = StringUtilssplit(abcdnulle  

  Systemoutprintln(strArraylength) 

  Systemoutprintln(strArraytoString()) 

  

  Systemoutprintln(判斷是否是某類字符 

  Systemoutprintln(StringUtilsisAlpha(ab)) 

  Systemoutprintln(StringUtilsisAlphanumeric()) 

  Systemoutprintln(StringUtilsisBlank()) 

  Systemoutprintln(StringUtilsisNumeric()) 

  } 

  

  public void systemUtilsDemo() { 

  Systemoutprintln(genHeader(SystemUtilsDemo)) 

  Systemoutprintln(獲得系統文件分隔符 

  Systemoutprintln(SystemUtilsFILE_SEPARATOR) 

  

  Systemoutprintln(獲得源文件編碼 

  Systemoutprintln(SystemUtilsFILE_ENCODING) 

  

  Systemoutprintln(獲得ext目錄 

  Systemoutprintln(SystemUtilsJAVA_EXT_DIRS) 

  

  Systemoutprintln(獲得java版本 

  Systemoutprintln(SystemUtilsJAVA_VM_VERSION) 

  

  Systemoutprintln(獲得java廠商 

  Systemoutprintln(SystemUtilsJAVA_VENDOR) 

  } 

  

  public void classUtilsDemo() { 

  Systemoutprintln(genHeader(ClassUtilsDemo)) 

  Systemoutprintln(獲取類實現的所有接口 

  Systemoutprintln(ClassUtilsgetAllInterfaces(Dateclass)) 

  

  Systemoutprintln(獲取類所有父類 

  Systemoutprintln(ClassUtilsgetAllSuperclasses(Dateclass)) 

  

  Systemoutprintln(獲取簡單類名 

  Systemoutprintln(ClassUtilsgetShortClassName(Dateclass)) 

  

  Systemoutprintln(獲取包名 

  Systemoutprintln(ClassUtilsgetPackageName(Dateclass)) 

  

  Systemoutprintln(判斷是否可以轉型 

  Systemoutprintln(ClassUtilsisAssignable(Dateclass Objectclass)) 

  Systemoutprintln(ClassUtilsisAssignable(Objectclass Dateclass)) 

  } 

  

  public void stringEscapeUtilsDemo(){ 

  Systemoutprintln(genHeader(StringEcsapeUtils)) 

  Systemoutprintln(轉換特殊字符 

  Systemoutprintln(html: + StringEscapeUtilsescapeHtml )) 

  Systemoutprintln(html: + StringEscapeUtilsescapeHtml )) 

  Systemoutprintln(html: + StringEscapeUtilsunescapeHtml)) 

  Systemoutprintln(html: + StringEscapeUtilsunescapeHtml)) 

  } 

  

  private final class BuildDemo { 

  String name; 

  int age; 

  

  public BuildDemo(String name int age) { 

  thisname = name; 

  thisage = age; 

  } 

  

  public String toString() { 

  ToStringBuilder tsb = new ToStringBuilder(this ToStringStyleMULTI_LINE_STYLE) 

  tsbappend(Name name) 

  tsbappend(Age age) 

  return tsbtoString() 

  } 

  

  public int hashCode() { 

  HashCodeBuilder hcb = new HashCodeBuilder() 

  hcbappend(name) 

  hcbappend(age) 

  return hcbhashCode() 

  } 

  

  public boolean equals(Object obj) { 

  if (!(obj instanceof BuildDemo)) { 

  return false; 

  } 

  BuildDemo bd = (BuildDemo) obj; 

  EqualsBuilder eb = new EqualsBuilder() 

  ebappend(name bdname) 

  ebappend(age bdage) 

  return ebisEquals() 

  } 

  } 

  

  public void builderDemo() { 

  Systemoutprintln(genHeader(BuilderDemo)) 

  BuildDemo obj = new BuildDemo(a  

  BuildDemo obj = new BuildDemo(b  

  BuildDemo obj = new BuildDemo(a  

  

  Systemoutprintln(toString() 

  Systemoutprintln(obj 

  Systemoutprintln(obj 

  Systemoutprintln(obj 

  

  Systemoutprintln(hashCode() 

  Systemoutprintln(objhashCode()) 

  Systemoutprintln(objhashCode()) 

  Systemoutprintln(objhashCode()) 

  

  Systemoutprintln(equals() 

  Systemoutprintln(objequals(obj)) 

  Systemoutprintln(objequals(obj)) 

  } 

  

  public void numberUtils() { 

  Systemoutprintln(genHeader(NumberUtils)) 

  Systemoutprintln(字符串轉為數字(不知道有什麼用) 

  Systemoutprintln(NumberUtilstoInt(ba )) 

  

  Systemoutprintln(從數組中選出最大值 

  Systemoutprintln(NumberUtilsmax(new int[] { })) 

  

  Systemoutprintln(判斷字符串是否全是整數 

  Systemoutprintln(NumberUtilsisDigits()) 

  

  Systemoutprintln(判斷字符串是否是有效數字 

  Systemoutprintln(NumberUtilsisNumber()) 

  } 

  

  public void dateFormatUtilsDemo() { 

  Systemoutprintln(genHeader(DateFormatUtilsDemo)) 

  Systemoutprintln(格式化日期輸出 

  Systemoutprintln(DateFormatUtilsformat(SystemcurrentTimeMillis() yyyyMMdd HH:mm:ss)) 

  

  Systemoutprintln(秒表 

  StopWatch sw = new StopWatch() 

  swstart() 

  

  for (Iterator iterator = erator(new Date() DateUtilsRANGE_WEEK_CENTER) iteratorhasNext()) { 

  Calendar cal = (Calendar) iteratornext() 

  Systemoutprintln(DateFormatUtilsformat(calgetTime() 

  yyMMdd HH:mm)) 

  } 

  

  swstop() 

  Systemoutprintln(秒表計時 + swgetTime()) 

  

  } 

  

  private String genHeader(String head) { 

  String[] header = new String[]; 

  header[] = StringUtilsrepeat(*  

  header[] = StringUtilscenter(  + head +   ^O^ 

  header[] = header[]; 

  return StringUtilsjoin(header \n 

  } 

  

  private void validateDemo(){ 

  String[] strarray = {a b c}; 

  Systemoutprintln(驗證功能 

  Systemoutprintln(ValidatenotEmpty(strarray)) 

  } 

  

  private void wordUtilsDemo(){ 

  Systemoutprintln(單詞處理功能 

  String str = wOrD

  String str = ghj\nui\tpo

  Systemoutprintln(WordUtilscapitalize(str))  //首字母大寫 

  Systemoutprintln(WordUtilscapitalizeFully(str))  //首字母大寫其它字母小寫 

  char[] ctrg = {}; 

  Systemoutprintln(WordUtilscapitalizeFully(i aMfine ctrg))  //在規則地方轉換 

  Systemoutprintln(WordUtilsinitials(str))  //獲取首字母 

  Systemoutprintln(WordUtilsinitials(Ben John Lee null))  //取每個單詞的首字母 

  char[] ctr = { }; 

  Systemoutprintln(WordUtilsinitials(Ben JLee ctr))  //按指定規則獲取首字母 

  Systemoutprintln(WordUtilsswapCase(str))  //大小寫逆轉 

  Systemoutprintln(WordUtilswrap(str ))  //解析\n和\t等字符 

  } 

  

  /**

  * @param args

  */ 

  public static void main(String[] args) { 

  TestLangDemo langDemo = new TestLangDemo() 

  

  langDemocharSetDemo() 

  langDemocharSetUtilsDemo() 

  langDemoobjectUtilsDemo() 

  langDemoserializationUtilsDemo() 

  langDemorandomStringUtilsDemo() 

  langDemostringUtilsDemo() 

  langDemosystemUtilsDemo() 

  langDemoclassUtilsDemo() 

  langDemostringEscapeUtilsDemo() 

  langDemobuilderDemo() 

  langDemonumberUtils() 

  langDemodateFormatUtilsDemo() 

  langDemovalidateDemo() 

  langDemowordUtilsDemo() 

  } 

    }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26856.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.