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 org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.CharSet;
import org.apache.commons.lang3.CharSetUtils;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.SerializationUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.text.WordUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.lang3.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 = 0;
for (int i = 0, len = demoStr.length(); i < len; i++) {
if (charSet.contains(demoStr.charAt(i))) {
count++;
}
}
System.out.println("count: " + count);
}
public void charSetUtilsDemo() {
System.out.println("**CharSetUtilsDemo**");
System.out.println("计算字符串中包含某字符数.");
System.out.println(CharSetUtils.count(
"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时,返回0长度字符串.");
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 reDate2 = (Date) SerializationUtils.deserialize(fis);
System.out.println(date.equals(reDate2));
} 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(500));
System.out.println("在指定字符串中生成长度为n的随机字符串.");
System.out.println(RandomStringUtils.random(5, "abcdefghijk"));
System.out.println("指定从字符或数字中生成随机字符串.");
System.out.println(RandomStringUtils.random(5, true, false));
System.out.println(RandomStringUtils.random(5, false, true));
}
public void stringUtilsDemo() {
System.out.println("**StringUtilsDemo**");
System.out.println("将字符串重复n次,将文字按某宽度居中,将字符串数组用某字符串连接.");
String[] header = new String[3];
header[0] = StringUtils.repeat("*", 50);
header[1] = StringUtils.center(" StringUtilsDemo ", 50, "^O^");
header[2] = header[0];
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.", 10));
System.out.println(StringUtils.abbreviate(
"The quick brown fox jumps over the lazy dog.", 15, 10));
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(StringUtils.containsOnly("aad", "aadd"));
System.out.println("检查一字符串是否不是另一字符串的子集.");
System.out.println(StringUtils.containsNone("defg", "aadd"));
System.out.println("检查一字符串是否包含另一字符串.");
System.out.println(StringUtils.contains("defg", "ef"));
System.out.println(StringUtils.containsOnly("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("12"));
System.out.println(StringUtils.isBlank(""));
System.out.println(StringUtils.isNumeric("123"));
}
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("html:" + StringEscapeUtils.escapeHtml3(" "));
System.out.println("html:" + StringEscapeUtils.escapeHtml4(" "));
System.out.println("html:" + StringEscapeUtils.unescapeHtml3("<p>"));
System.out.println("html:" + StringEscapeUtils.unescapeHtml4("<p>"));
}
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 obj1 = new BuildDemo("a", 1);
BuildDemo obj2 = new BuildDemo("b", 2);
BuildDemo obj3 = new BuildDemo("a", 1);
System.out.println("toString()");
System.out.println(obj1);
System.out.println(obj2);
System.out.println(obj3);
System.out.println("hashCode()");
System.out.println(obj1.hashCode());
System.out.println(obj2.hashCode());
System.out.println(obj3.hashCode());
System.out.println("equals()");
System.out.println(obj1.equals(obj2));
System.out.println(obj1.equals(obj3));
}
public void numberUtils() {
System.out.println(genHeader("NumberUtils"));
System.out.println("字符串转为数字(不知道有什么用).");
System.out.println(NumberUtils.toInt("ba", 33));
System.out.println("从数组中选出最大值.");
System.out.println(NumberUtils.max(new int[] { 1, 2, 3, 4 }));
System.out.println("判断字符串是否全是整数.");
System.out.println(NumberUtils.isDigits("123.1"));
System.out.println("判断字符串是否是有效数字.");
System.out.println(NumberUtils.isNumber("0123.1"));
}
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(DateFormatUtils.format(new Date(), "yyyy-MM-dd")
);
System.out.println("秒表.");
StopWatch sw = new StopWatch();
sw.start();
for (Iterator iterator = DateUtils.iterator(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[3];
header[0] = StringUtils.repeat("*", 50);
header[1] = StringUtils.center(" " + head + " ", 50, "^O^");
header[2] = header[0];
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 str1 = "wOrD";
String str2 = "ghj\nui\tpo";
System.out.println(WordUtils.capitalize(str1)); //首字母大写
System.out.println(WordUtils.capitalizeFully(str1)); //首字母大写其它字母小写
char[] ctrg = {'.'};
System.out.println(WordUtils.capitalizeFully("i aM.fine", ctrg)); //在规则地方转换
System.out.println(WordUtils.initials(str1)); //获取首字母
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(str1)); //大小写逆转
System.out.println(WordUtils.wrap(str2, 1)); //解析\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();
}
}
分享到:
相关推荐
Apache Commons Lang 是一个 Java 类库,它为 Java SDK 增加了大量有用的实用工具类,弥补了标准库中的一些不足。"commons-lang3-3.0-bin.zip" 是 Apache Commons Lang 的第 3.0 版本的二进制分发包。这个压缩包包含...
Apache Commons Lang 2.4版是该库的一个特定版本,可能包含了一些特定的修复、改进和新特性。例如,它可能已经修复了前一版本中的一些已知问题,或者增加了新的实用工具类。具体到2.4版本,可能包括了对字符串处理的...
将`commons-lang3-3.1.jar`加入到项目的类路径中,可以直接使用Lang库提供的各种工具类。对于Maven项目,可以在pom.xml文件中添加依赖: ```xml <groupId>org.apache.commons <artifactId>commons-lang3 ...
"commons-lang-2.6.jar" 是Apache Commons Lang项目的版本2.6的Java类库,这个库提供了大量的实用工具类,极大地丰富了Java标准库的功能。Apache Commons Lang是Apache软件基金会开发的一个开源项目,旨在为Java开发...
Apache Commons Lang 是一个Java工具包,它提供了一些实用的、补充了Java标准库的类。在"commons-lang3-3.4.jar"这个压缩包中,我们关注的是Apache Commons Lang 3.4版本,它是该项目的一个重要里程碑。此库包含了...
Apache Commons Lang 是一个Java工具包,它提供了一些实用的辅助方法,补充了Java核心库的功能。在本案例中,我们关注的是"commons-lang3-3.8.1.jar",这是Apache Commons Lang项目的版本3.8.1的归档文件。这个JAR...
《Apache Commons Lang 2.6:Web开发的强大工具》 Apache Commons Lang是Java开发者们广泛使用的开源库之一,其中的2.6版本以其...无论是在小型项目还是大型企业级应用中,Lang 2.6都展现出了其强大的价值和实用性。
Apache Commons Lang 是一个Java工具包,它提供了一些实用的、高级的编程功能,这些功能在标准Java库中并未涵盖。`commons-lang3-3.5`是该项目的一个版本,主要包含了许多增强和补充了Java核心类库的工具类。这个...
应用于java技术spring mvc里面的常用jar包
标题中的"commons-lang3-3.3.2"指的是Apache Commons Lang库的版本3.3.2。这个库是Java编程语言中一个非常重要的工具集,它提供了许多实用的类和方法,以增强Java标准库的功能。Apache Commons Lang是Apache软件基金...
《Apache Commons Lang 3.5源码解析》 Apache Commons Lang是Java开发中不可或缺的工具库,其中Lang 3.5版本提供了大量的实用类和方法,极大地简化了日常编程任务。本文将深入探讨其源码,揭示其内部设计思想和实现...
《Apache Commons Lang 2.6在Android开发中的应用与详解》 Apache Commons Lang是Java编程领域的一个重要组件库,它提供了许多实用的工具类和方法,极大地丰富了Java标准库的功能。在Android开发中, Commons Lang ...
Apache Commons Lang 是一个Java工具包,它提供了对Java语言核心类库的扩展,以增加一些在标准库中未涵盖的功能。这个"commons-lang3-3.9-bin.zip"压缩包包含了Apache Commons Lang 3.9版本的二进制文件。Apache ...
《Apache Commons Lang 3.3.2:源码解析与应用探索》 Apache Commons Lang 是一个Java编程语言的实用工具库,它提供了大量的类和方法来扩展Java的标准库。在这个压缩包"commons-lang3-3.3.2.rar"中,我们主要关注的...
接下来是`commons-lang.jar`,这是Apache Commons项目中的另一个重要组件,主要处理Java语言层面上的一些通用任务。它包含了一系列增强Java语言功能的工具类,例如字符串操作、日期时间处理、数组操作、枚举支持等。...
在Java Web开发中,`commons-lang3-3.7`通常会被部署在Web应用服务器的lib目录下,作为项目的基础库,为各种业务场景提供便捷的支持。通过合理利用这些工具类,开发者可以提高代码的可读性、可维护性和性能,降低...
Apache Commons Lang库广泛应用于Java开发,尤其在需要进行复杂字符串、数组、日期处理时,它的功能可以帮助开发人员提高效率,减少错误。使用该库时,只需通过Maven或Gradle等构建工具添加对应的依赖,即可在项目中...
Aduna Commons Lang是一个开源项目,它由Aduna组织维护,致力于提供Java语言的一些通用功能和实用工具。这些工具涵盖了字符串处理、日期和时间操作、数学计算、反射、枚举支持等多个方面,极大地增强了Java开发者的...
标题中的"aduna-commons-lang-2.9.0.jar.zip"是一个压缩文件,它包含了一个名为"aduna-commons-lang-2.9.0.jar"的Java档案(JAR)文件。这个JAR文件是Java平台上的一个可执行文件格式,通常用于封装Java类库。"aduna...
《Aduna Commons Lang 2.7.0:深入解析与应用》 在Java开发领域,库和框架的使用是常见的,它们提供了丰富的功能,帮助开发者更高效地完成工作。其中,Aduna Commons Lang是一个非常实用的工具包,它扩展了Java核心...