知识大全 Apache Commons Lang 包

Posted 字符

篇首语:事亲尽教自天成,鉴本无尘水本清。相彼禽兮犹学习,灵乌反哺更分明。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Apache Commons Lang 包相关的知识,希望对你有一定的参考价值。

Apache Commons Lang 包  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  Apache Commons Lang 包是用来处理Java基本对象方法的工具类包 可以简化很多平时经常要用到的写法 比如

  SerializationUtils类 为序列化工具类 也是lang包下的工具 主要用于序列化操作 同时提供对象克隆接口

  ToStringBuilder类 功能就是在自己定义一个类的toString 方法时 方便的格式化类的属性 ToStringBuilder类中的append方法可以向该类添加基本类型 数组和对象 只有添加的方法才会被toString方法输出 ToStringStyle类则是对输出的内容进行格式化

  EqualsBuilder与HashCodeBuilder类 可以简化Java类中equals与hashCode方法的改写过程

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

   import java io File;

     import java io FileInputStream;

     import java io FileNotFoundException;

     import java io FileOutputStream;

     import java io IOException;

     import java util Calendar;

     import java util Date;

     import java util Iterator;

  

     import mons lang ArrayUtils;

     import mons lang CharSet;

     import mons lang CharSetUtils;

     import mons lang ClassUtils;

     import mons lang ObjectUtils;

     import mons lang RandomStringUtils;

     import mons lang SerializationUtils;

     import mons lang StringEscapeUtils;

     import mons lang StringUtils;

     import mons lang SystemUtils;

     import mons lang Validate;

     import mons lang builder EqualsBuilder;

     import mons lang builder HashCodeBuilder;

     import mons lang builder ToStringBuilder;

     import mons lang builder ToStringStyle;

     import mons lang math NumberUtils;

     import mons lang text WordUtils;

     import mons lang time DateFormatUtils;

     import mons lang time DateUtils;

     import mons lang time StopWatch;

  

     public class TestLangDemo

  

  

  public void charSetDemo()

  

  System out println( **CharSetDemo** )

  

  CharSet charSet = CharSet getInstance( aeiou )

  

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

  

  int count = ;

  

  for (int i = len = demoStr length() i < len; i++)

  

  if (ntains(demoStr charAt(i)))

  

  count++;

  

  

  

  

  

  System out println( count: + count)

  

  

  

  

  public void charSetUtilsDemo()

  

  System out println( **CharSetUtilsDemo** )

  

  System out println( 计算字符串中包含某字符数 )

  

  System out println(unt(

  

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

  

  

  System out println( 删除字符串中某字符 )

  

  System out println(CharSetUtils delete(

  

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

  

  

  System out println( 保留字符串中某字符 )

  

  System out println(CharSetUtils keep(

  

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

  

  

  System out println( 合并重复的字符 )

  

  System out println(CharSetUtils squeeze( a  bbbbbb

  c dd b d ))

  

  

  

  

  public void objectUtilsDemo()

  

  System out println( **ObjectUtilsDemo** )

  

  System out println( Object为null时 默认打印某字符 )

  

  Object obj = null;

  

  System out println(ObjectUtils defaultIfNull(obj 空 ))

  

  

  System out println( 验证两个引用是否指向的Object是否相等 取决于Object的equals()方法 )

  

  Object a = new Object()

  

  Object b = a;

  

  Object c = new Object()

  

  System out println(ObjectUtils equals(a b))

  

  System out println(ObjectUtils equals(a c))

  

  

  System out println( 用父类Object的toString()方法返回对象信息 )

  

  Date date = new Date()

  

  System out println(ObjectUtils identityToString(date))

  

  System out println(date)

  

  

  System out println( 返回类本身的toString()方法结果 对象为null时 返回 长度字符串 )

  

  System out println(ObjectUtils toString(date))

  

  System out println(ObjectUtils toString(null))

  

  System out println(date)

  

  

  

  

  public void serializationUtilsDemo()

  

  System out println( *SerializationUtils** )

  

  Date date = new Date()

  

  byte[] bytes = SerializationUtils serialize(date)

  

  System out println(ArrayUtils toString(bytes))

  

  System out println(date)

  

  

  Date reDate = (Date) SerializationUtils deserialize(bytes)

  

  System out println(reDate)

  

  System out println(ObjectUtils equals(date reDate))

  

  System out println(date == reDate)

  

  

  FileOutputStream fos = null; 

  FileInputStream fis = null; 

  try  

  fos = new FileOutputStream(new File( d:/test txt ))  

  fis = new FileInputStream(new File( d:/test txt ))  

  SerializationUtils serialize(date fos)  

  Date reDate = (Date) SerializationUtils deserialize(fis)  

  

  System out println(date equals(reDate ))  

  

   catch (FileNotFoundException e)  

  e printStackTrace()  

   finally  

  try  

  fos close()  

  fis close()  

   catch (IOException e)  

  e printStackTrace()  

   

   

  

   

  

  public void randomStringUtilsDemo()  

  System out println( **RandomStringUtilsDemo** )  

  System out println( 生成指定长度的随机字符串 好像没什么用 )  

  System out println(RandomStringUtils random( ))  

  

  System out println( 在指定字符串中生成长度为n的随机字符串 )  

  System out println(RandomStringUtils random( abcdefghijk ))  

  

  System out println( 指定从字符或数字中生成随机字符串 )  

  System out println(RandomStringUtils random( true false))  

  System out println(RandomStringUtils random( false true))  

  

   

  

  public void stringUtilsDemo()  

  System out println( **StringUtilsDemo** )  

  System out println( 将字符串重复n次 将文字按某宽度居中 将字符串数组用某字符串连接 )  

  String[] header = new String[ ]; 

  header[ ] = StringUtils repeat( * )  

  header[ ] = StringUtils center(   StringUtilsDemo  ^O^ )  

  header[ ] = header[ ]; 

  String head = StringUtils join(header \\n )  

  System out println(head)  

  

  System out println( 缩短到某长度 用…结尾 )  

  System out println(StringUtils abbreviate( 

   The quick brown fox jumps over the lazy dog ))  

  System out println(StringUtils abbreviate( 

   The quick brown fox jumps over the lazy dog ))  

  

  System out println( 返回两字符串不同处索引号 )  

  System out println(StringUtils indexOfDifference( aaabc aaacc ))  

  

  System out println( 返回两字符串不同处开始至结束 )  

  System out println(StringUtils difference( aaabcde aaaccde ))  

  

  System out println( 截去字符串为以指定字符串结尾的部分 )  

  System out println(StringUtils chomp( aaabcde de ))  

  

  System out println( 检查一字符串是否为另一字符串的子集 )  

  System out println(ntainsOnly( aad aadd ))  

  

  System out println( 检查一字符串是否不是另一字符串的子集 )  

  System out println(ntainsNone( defg aadd ))  

  

  System out println( 检查一字符串是否包含另一字符串 )  

  System out println(ntains( defg ef ))  

  System out println(ntainsOnly( ef defg ))  

  

  System out println( 返回可以处理null的toString() )  

  System out println(StringUtils defaultString( aaaa ))  

  System out println( ? + StringUtils defaultString(null) + ! )  

  

  System out println( 去除字符中的空格 )  

  System out println(StringUtils deleteWhitespace( aa  bb  cc ))  

  

  System out println( 分隔符处理成数组 )  

  String[] strArray = StringUtils split( a b c d null e )  

  System out println(strArray length)  

  System out println(strArray toString())  

  

  System out println( 判断是否是某类字符 )  

  System out println(StringUtils isAlpha( ab ))  

  System out println(StringUtils isAlphanumeric( ))  

  System out println(StringUtils isBlank( ))  

  System out println(StringUtils isNumeric( ))  

   

  

  public void systemUtilsDemo()  

  System out println(genHeader( SystemUtilsDemo ))  

  System out println( 获得系统文件分隔符 )  

  System out println(SystemUtils FILE_SEPARATOR)  

  

  System out println( 获得源文件编码 )  

  System out println(SystemUtils FILE_ENCODING)  

  

  System out println( 获得ext目录 )  

  System out println(SystemUtils JAVA_EXT_DIRS)  

  

  System out println( 获得java版本 )  

  System out println(SystemUtils JAVA_VM_VERSION)  

  

  System out println( 获得java厂商 )  

  System out println(SystemUtils JAVA_VENDOR)  

   

  

  public void classUtilsDemo()  

  System out println(genHeader( ClassUtilsDemo ))  

  System out println( 获取类实现的所有接口 )  

  System out println(ClassUtils getAllInterfaces(Date class))  

  

  System out println( 获取类所有父类 )  

  System out println(ClassUtils getAllSuperclasses(Date class))  

  

  System out println( 获取简单类名 )  

  System out println(ClassUtils getShortClassName(Date class))  

  

  System out println( 获取包名 )  

  System out println(ClassUtils getPackageName(Date class))  

  

  System out println( 判断是否可以转型 )  

  System out println(ClassUtils isAssignable(Date class Object class))  

  System out println(ClassUtils isAssignable(Object class Date class))  

   

  

  public void stringEscapeUtilsDemo() 

  System out println(genHeader( StringEcsapeUtils ))  

  System out println( 转换特殊字符 )  

  System out println( : + StringEscapeUtils escapeHtml ( ))  

  System out println( : + StringEscapeUtils escapeHtml ( ))  

  System out println( : + StringEscapeUtils unescapeHtml ( ))  

  System out println( : + StringEscapeUtils unescapeHtml ( ))  

   

  

  private final class BuildDemo  

  String name; 

  int age; 

  

  public BuildDemo(String name int age)  

  this name = name; 

  this age = age; 

   

  

  public String toString()  

  ToStringBuilder tsb = new ToStringBuilder(this ToStringStyle MULTI_LINE_STYLE)  

  tsb append( Name name)  

  tsb append( Age age)  

  return tsb toString()  

   

  

  public int hashCode()  

  HashCodeBuilder hcb = new HashCodeBuilder()  

  hcb append(name)  

  hcb append(age)  

  return hcb hashCode()  

   

  

  public boolean equals(Object obj)  

  if (!(obj instanceof BuildDemo))  

  return false; 

   

  BuildDemo bd = (BuildDemo) obj; 

  EqualsBuilder eb = new EqualsBuilder()  

  eb append(name bd name)  

  eb append(age bd age)  

  return eb isEquals()  

   

   

  

  public void builderDemo()  

  System out println(genHeader( BuilderDemo ))  

  BuildDemo obj = new BuildDemo( a )  

  BuildDemo obj = new BuildDemo( b )  

  BuildDemo obj = new BuildDemo( a )  

  

  System out println( toString() )  

  System out println(obj )  

  System out println(obj )  

  System out println(obj )  

  

  System out println( hashCode() )  

  System out println(obj hashCode())  

  System out println(obj hashCode())  

  System out println(obj hashCode())  

  

  System out println( equals() )  

  System out println(obj equals(obj ))  

  System out println(obj equals(obj ))  

   

  

  public void numberUtils()  

  System out println(genHeader( NumberUtils ))  

  System out println( 字符串转为数字(不知道有什么用) )  

  System out println(NumberUtils toInt( ba ))  

  

  System out println( 从数组中选出最大值 )  

  System out println(NumberUtils max(new int[] ))  

  

  System out println( 判断字符串是否全是整数 )  

  System out println(NumberUtils isDigits( ))  

  

  System out println( 判断字符串是否是有效数字 )  

  System out println(NumberUtils isNumber( ))  

   

  

  public void dateFormatUtilsDemo()  

  System out println(genHeader( DateFormatUtilsDemo ))  

  System out println( 格式化日期输出 )  

  System out println(DateFormatUtils format(System currentTimeMillis() yyyy MM dd HH:mm:ss ))  

  

  System out println( 秒表 )  

  StopWatch sw = new StopWatch()  

  sw start()  

  

  for (Iterator iterator = erator(new Date() DateUtils RANGE_WEEK_CENTER) iterator hasNext() )  

  Calendar cal = (Calendar) iterator next()  

  System out println(DateFormatUtils format(cal getTime()  

   yy MM dd HH:mm ))  

   

  

  sw stop()  

  System out println( 秒表计时 + sw getTime())  

  

   

  

  private String genHeader(String head)  

  String[] header = new String[ ]; 

  header[ ] = StringUtils repeat( * )  

  header[ ] = StringUtils center(   + head +   ^O^ )  

  header[ ] = header[ ]; 

  return StringUtils join(header \\n )  

   

  

  private void validateDemo() 

  String[] strarray = a b c ; 

  System out println( 验证功能 )  

  System out println(Validate notEmpty(strarray))  

   

  

  private void wordUtilsDemo() 

  System out println( 单词处理功能 )  

  String str = wOrD ; 

  String str = ghj\\nui\\tpo ; 

  System out println(WordUtils capitalize(str ))   //首字母大写 

  System out println(WordUtils capitalizeFully(str ))   //首字母大写其它字母小写 

  char[] ctrg = ; 

  System out println(WordUtils capitalizeFully( i aM fine ctrg))   //在规则地方转换 

  System out println(WordUtils initials(str ))   //获取首字母 

  System out println(WordUtils initials( Ben John Lee null))   //取每个单词的首字母 

  char[] ctr = ; 

  System out println(WordUtils initials( Ben J Lee ctr))   //按指定规则获取首字母 

  System out println(WordUtils swapCase(str ))   //大小写逆转 

  System out println(WordUtils wrap(str ))   //解析\\n和\\t等字符 

   

  

  /**

  * @param args

  */ 

  public static void main(String[] args)  

  TestLangDemo langDemo = new TestLangDemo()  

  

  langDemo charSetDemo()  

  langDemo charSetUtilsDemo()  

  langDemo objectUtilsDemo()  

  langDemo serializationUtilsDemo()  

  langDemo randomStringUtilsDemo()  

  langDemo stringUtilsDemo()  

  langDemo systemUtilsDemo()  

  langDemo classUtilsDemo()  

  langDemo stringEscapeUtilsDemo()  

  langDemo builderDemo()  

  langDemo numberUtils()  

  langDemo dateFormatUtilsDemo()  

  langDemo validateDemo()  

  langDemo wordUtilsDemo()  

   

cha138/Article/program/Java/hx/201311/26856

相关参考

知识大全 Apache Commons DbUtils 快速上手

ApacheCommonsDbUtils快速上手  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!Hi

知识大全 struts源代码阅读(Commons-Pool包)

struts源代码阅读(Commons-Pool包)  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

知识大全 struts源代码阅读(Commons-Beanutils包)

struts源代码阅读(Commons-Beanutils包)  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来

知识大全 java.lang.String类运算符重载

java.lang.String类运算符重载  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  Gi

知识大全 使用 Jakarta Commons 之库组件攻略

使用JakartaCommons之库组件攻略  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  Ja

知识大全 Apusic AS的Web应用中调用commons-logging

ApusicAS的Web应用中调用commons-logging  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起

知识大全 Apusic AS的Web应用中调用commons-loggi

ApusicAS的Web应用中调用commons-loggi  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看

知识大全 隐藏apache和php的版本信息配置方法

  隐藏apache和php的版本信息webserver避免一些不必要的麻烦可以把apache和php的版本信息不显示  隐藏Apache版本信息  /etc/apache/apacheconf或/e

知识大全 js动态为代码着色显示行号

 代码如下:<!DOCTYPEPUBLIC"//WC//DTDXHTMLStrict//EN""<xmlns=":lang="en"lang="en"><head&g

知识大全 apache设置静态文件缓存方法介绍

  为了减少客户端对服务端资源的请求可以开启mod_expiresso模块  在apache%C%E%D%C/"target="_blank">apache配置文件中去掉这段  “#LoadMo