服务器

位置:IT落伍者 >> 服务器 >> 浏览文章

Apache Commons Lang 包


发布日期:2019年03月21日
 
Apache Commons Lang 包

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()

}

}

               

上一篇:Java Socket多线程如何支持服务器模型

下一篇:使用Java实现FTP服务器