http://www.open-open.com/bbs/view/1318864045468
http://www.oschina.net/code/snippet_2765_14434
这一组API的所有包名都以org.apache.commons.lang开头,共有如下8个包:
org.apache.commons.lang
org.apache.commons.lang.builder
org.apache.commons.lang.enum
org.apache.commons.lang.enums
org.apache.commons.lang.exception
org.apache.commons.lang.math
org.apache.commons.lang.mutable
org.apache.commons.lang.time
其中的lang.enum已不建议使用,替代它的是紧随其后的lang.enums包。 lang包主要是一些可以高度重用的Util类;lang.builder包包含了一组用于产生每个Java类中都常使用到的toString()、 hashCode()、equals()、compareTo()等等方法的构造器;lang.enums包顾名思义用于处理枚 举;lang.exception包用于处理Java标准API中的exception,为1.4之前版本提供Nested Exception功能;lang.math包用于处理数字;lang.mutable用于包装值型变量;lang.time包提供处理日期和时间的功 能。
由于Commons的包和类实在很多,不可能一个一个讲了,在接下来的专题文章中我就只分别过一下lang、lang.builder、lang.math和lang.time这几个包和常见的用法,其他的我们可以在用到时临时参考一下Javadoc。位置就在安装路径的
…\commons-lang-2.1\docs\api\index.html
我们首先来看org.apache.commons.lang包,这个包提供了一些有用的包含static方法的Util类。除了6个Exception类和2个已经deprecated的数字类之外,commons.lang包共包含了17个实用的类:
ArrayUtils – 用于对数组的操作,如添加、查找、删除、子数组、倒序、元素类型转换等;
BitField – 用于操作位元,提供了一些方便而安全的方法;
BooleanUtils – 用于操作和转换boolean或者Boolean及相应的数组;
CharEncoding – 包含了Java环境支持的字符编码,提供是否支持某种编码的判断;
CharRange – 用于设定字符范围并做相应检查;
CharSet – 用于设定一组字符作为范围并做相应检查;
CharSetUtils – 用于操作CharSet;
CharUtils – 用于操作char值和Character对象;
ClassUtils – 用于对Java类的操作,不使用反射;
ObjectUtils – 用于操作Java对象,提供null安全的访问和其他一些功能;
RandomStringUtils – 用于生成随机的字符串;
SerializationUtils – 用于处理对象序列化,提供比一般Java序列化更高级的处理能力;
StringEscapeUtils – 用于正确处理转义字符,产生正确的Java、JavaScript、HTML、XML和SQL代码;
StringUtils – 处理String的核心类,提供了相当多的功能;
SystemUtils – 在java.lang.System基础上提供更方便的访问,如用户路径、Java版本、时区、操作系统等判断;
Validate – 提供验证的操作,有点类似assert断言;
WordUtils – 用于处理单词大小写、换行等。
- 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("秒表.");
- 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();
- }
- }
相关推荐
通用查询解析器用于一般查询语法的愚蠢解析器。 snake plissken -title:"L. A."我想查找与Snake Plissken或有关的电影,而不是1996年的电影《逃离洛杉矶》...niventitle:"The Long ARM of Gil Hamilton"与术语一样,
----------------------- 'False_VB教程:VB制作带...Public Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam ...
mysql-community-common-5.7.28-1.el7.x86_64.rpm mysql-community-libs-5.7.28-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.28-1.el7.x86_64.rpm mysql-community-server-5.7.28-1.el7.x86_64.rpm 依赖包 ...
这插件依赖的几个jar包 1、jetty-http-9.1.2.v20140210.jar 2、jetty-io-9.1.2.v20140210.jar 3、jetty-util-9.1.2.v20140210.jar 4、websocket-api-9.1.1.v20140108.jar 5、websocket-client-9.1.1.v20140108.jar 6...
longToIpv4()将long转换为ipv4地址,反方法是ipv4ToLong() netCat()简易的数据发送方法 5、com.baijob.commonTools.net.SSHUtil SSH相关工具类 getSession()获得一个SSH会话 bindPort()将远程主机的端口映射到...
必须使用的jar包。为JRE5.0+的更好的版本所提供 Jar文件包含的类: META-INF/MANIFEST.MFMETA-INF/LICENSE.txtMETA-INF/NOTICE.txtorg.apache.commons.lang.ArrayUtils.class org.apache.commons.lang.BitField....
Common Valid Keywords: userid = username/password@tnsname data = 数据文件 (默认dts.oz) log = 处理日志文件 control = 控制文件 columnarrayrows = 直接路径字段数组行数,5-5000 (默认5000) columnover ...
apache提供的众多commons工具包,号称Java第二API,而common里面lang3包更是被我们使用得最多的,希望以后大家使用此工具包,写出优雅的代码
Common4Android是一个通用Android工具库,包含网络库、线程管理器、常用Util工具、热修复,它拥有良好的架构,低耦合、高内聚,使用起来非常轻松。 -基类 ClassName Description ...
-下载后解压zip包,将commons-fileupload-1.1.1.jar,和commons-io-1.2.jar(这里我们用的是更新的版本,但是用法是一样的)复制到tomcat的webapps\你的webapp\WEB-INF\lib\下,如果目录不存在请自建目录。 新建一个...
Q440760 - Export to PDF - The File Save dialog is not shown if the default file name for a report is too long B218629 - In the Page Number Format dialog, the "Start At" field value is incorrectly ...
Q440760 - Export to PDF - The File Save dialog is not shown if the default file name for a report is too long B218629 - In the Page Number Format dialog, the "Start At" field value is incorrectly ...
framerate = get_framerate((long long)(buf.timestamp.tv_sec),(long long)(buf.timestamp.tv_usec)); if(framerate > 1.0){ cap->cap_fps = framerate; //hv_dbg("framerate: %0.2ffps\n",cap->cap_fps); } ...
Q440760 - Export to PDF - The File Save dialog is not shown if the default file name for a report is too long B218629 - In the Page Number Format dialog, the "Start At" field value is incorrectly ...
Q440760 - Export to PDF - The File Save dialog is not shown if the default file name for a report is too long B218629 - In the Page Number Format dialog, the "Start At" field value is incorrectly ...
Q440760 - Export to PDF - The File Save dialog is not shown if the default file name for a report is too long B218629 - In the Page Number Format dialog, the "Start At" field value is incorrectly ...
Q440760 - Export to PDF - The File Save dialog is not shown if the default file name for a report is too long B218629 - In the Page Number Format dialog, the "Start At" field value is incorrectly ...
This version is the result of long unactivity of RX Library authors and some imperfections and bugs of other RX adaptations to Delphi 6. The authors of this version disclaim all warranties as to ...
se.kodapan.osm.domain:OsmObject #getId():long #setTag(key, value) #getTag(key)se.kodapan.osm.domain:Node extends OsmObjectse.kodapan.osm.domain:Way extends OsmObjectse.kodapan.osm.domain:Relation ...
此包包含 Long Track 的源代码,如下所述: Yen-Pin Chiu、Yong-Siang Shih、Yang-Yin Lee、Chih-Chieh Shao、Ming-Lun Cai、Sheng-Lun Wei 和 Hsin-Hsi Chen。 在 2014 年 ERD 挑战赛中识别和消除长短文本实体...