看例子练apache commons之lang篇

简介: 看commons包时的相关练习,可以用这个包提高编码效率。 package cn.org.c4c.test.apache; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputS

看commons包时的相关练习,可以用这个包提高编码效率。

  1. package cn.org.c4c.test.apache;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.util.Calendar;
  8. import java.util.Date;
  9. import java.util.Iterator;
  10. import org.apache.commons.lang.ArrayUtils;
  11. import org.apache.commons.lang.CharSet;
  12. import org.apache.commons.lang.CharSetUtils;
  13. import org.apache.commons.lang.ClassUtils;
  14. import org.apache.commons.lang.ObjectUtils;
  15. import org.apache.commons.lang.RandomStringUtils;
  16. import org.apache.commons.lang.SerializationUtils;
  17. import org.apache.commons.lang.StringEscapeUtils;
  18. import org.apache.commons.lang.StringUtils;
  19. import org.apache.commons.lang.SystemUtils;
  20. import org.apache.commons.lang.builder.EqualsBuilder;
  21. import org.apache.commons.lang.builder.HashCodeBuilder;
  22. import org.apache.commons.lang.builder.ToStringBuilder;
  23. import org.apache.commons.lang.builder.ToStringStyle;
  24. import org.apache.commons.lang.math.NumberUtils;
  25. import org.apache.commons.lang.time.DateFormatUtils;
  26. import org.apache.commons.lang.time.DateUtils;
  27. import org.apache.commons.lang.time.StopWatch;
  28. public class LangDemo {
  29.     public void charSetDemo() {
  30.         System.out.println("**CharSetDemo**");
  31.         CharSet charSet = CharSet.getInstance("aeiou");
  32.         String demoStr = "The quick brown fox jumps over the lazy dog.";
  33.         int count = 0;
  34.         for (int i = 0, len = demoStr.length(); i < len; i++) {
  35.             if (charSet.contains(demoStr.charAt(i))) {
  36.                 count++;
  37.             }
  38.         }
  39.         System.out.println("count: " + count);
  40.     }
  41.     public void charSetUtilsDemo() {
  42.         System.out.println("**CharSetUtilsDemo**");
  43.         System.out.println("计算字符串中包含某字符数.");
  44.         System.out.println(CharSetUtils.count(
  45.                 "The quick brown fox jumps over the lazy dog.""aeiou"));
  46.         System.out.println("删除字符串中某字符.");
  47.         System.out.println(CharSetUtils.delete(
  48.                 "The quick brown fox jumps over the lazy dog.""aeiou"));
  49.         System.out.println("保留字符串中某字符.");
  50.         System.out.println(CharSetUtils.keep(
  51.                 "The quick brown fox jumps over the lazy dog.""aeiou"));
  52.         System.out.println("合并重复的字符.");
  53.         System.out.println(CharSetUtils.squeeze("a  bbbbbb     c dd""b d"));
  54.     }
  55.     public void objectUtilsDemo() {
  56.         System.out.println("**ObjectUtilsDemo**");
  57.         System.out.println("Object为null时,默认打印某字符.");
  58.         Object obj = null;
  59.         System.out.println(ObjectUtils.defaultIfNull(obj, "空"));
  60.         System.out.println("验证两个引用是否指向的Object是否相等,取决于Object的equals()方法.");
  61.         Object a = new Object();
  62.         Object b = a;
  63.         Object c = new Object();
  64.         System.out.println(ObjectUtils.equals(a, b));
  65.         System.out.println(ObjectUtils.equals(a, c));
  66.         System.out.println("用父类Object的toString()方法返回对象信息.");
  67.         Date date = new Date();
  68.         System.out.println(ObjectUtils.identityToString(date));
  69.         System.out.println(date);
  70.         System.out.println("返回类本身的toString()方法结果,对象为null时,返回0长度字符串.");
  71.         System.out.println(ObjectUtils.toString(date));
  72.         System.out.println(ObjectUtils.toString(null));
  73.         System.out.println(date);
  74.     }
  75.     public void serializationUtilsDemo() {
  76.         System.out.println("*SerializationUtils**");
  77.         Date date = new Date();
  78.         byte[] bytes = SerializationUtils.serialize(date);
  79.         System.out.println(ArrayUtils.toString(bytes));
  80.         System.out.println(date);
  81.         Date reDate = (Date) SerializationUtils.deserialize(bytes);
  82.         System.out.println(reDate);
  83.         System.out.println(ObjectUtils.equals(date, reDate));
  84.         System.out.println(date == reDate);
  85.         FileOutputStream fos = null;
  86.         FileInputStream fis = null;
  87.         try {
  88.             fos = new FileOutputStream(new File("d:/test.txt"));
  89.             fis = new FileInputStream(new File("d:/test.txt"));
  90.             SerializationUtils.serialize(date, fos);
  91.             Date reDate2 = (Date) SerializationUtils.deserialize(fis);
  92.             System.out.println(date.equals(reDate2));
  93.         } catch (FileNotFoundException e) {
  94.             e.printStackTrace();
  95.         } finally {
  96.             try {
  97.                 fos.close();
  98.                 fis.close();
  99.             } catch (IOException e) {
  100.                 e.printStackTrace();
  101.             }
  102.         }
  103.     }
  104.     public void randomStringUtilsDemo() {
  105.         System.out.println("**RandomStringUtilsDemo**");
  106.         System.out.println("生成指定长度的随机字符串,好像没什么用.");
  107.         System.out.println(RandomStringUtils.random(500));
  108.         System.out.println("在指定字符串中生成长度为n的随机字符串.");
  109.         System.out.println(RandomStringUtils.random(5"abcdefghijk"));
  110.         System.out.println("指定从字符或数字中生成随机字符串.");
  111.         System.out.println(RandomStringUtils.random(5truefalse));
  112.         System.out.println(RandomStringUtils.random(5falsetrue));
  113.     }
  114.     public void stringUtilsDemo() {
  115.         System.out.println("**StringUtilsDemo**");
  116.         System.out.println("将字符串重复n次,将文字按某宽度居中,将字符串数组用某字符串连接.");
  117.         String[] header = new String[3];
  118.         header[0] = StringUtils.repeat("*"50);
  119.         header[1] = StringUtils.center("  StringUtilsDemo  "50"^O^");
  120.         header[2] = header[0];
  121.         String head = StringUtils.join(header, "/n");
  122.         System.out.println(head);
  123.         System.out.println("缩短到某长度,用...结尾.");
  124.         System.out.println(StringUtils.abbreviate(
  125.                 "The quick brown fox jumps over the lazy dog."10));
  126.         System.out.println(StringUtils.abbreviate(
  127.                 "The quick brown fox jumps over the lazy dog."1510));
  128.         System.out.println("返回两字符串不同处索引号.");
  129.         System.out.println(StringUtils.indexOfDifference("aaabc""aaacc"));
  130.         System.out.println("返回两字符串不同处开始至结束.");
  131.         System.out.println(StringUtils.difference("aaabcde""aaaccde"));
  132.         System.out.println("截去字符串为以指定字符串结尾的部分.");
  133.         System.out.println(StringUtils.chomp("aaabcde""de"));
  134.         System.out.println("检查一字符串是否为另一字符串的子集.");
  135.         System.out.println(StringUtils.containsOnly("aad""aadd"));
  136.         System.out.println("检查一字符串是否不是另一字符串的子集.");
  137.         System.out.println(StringUtils.containsNone("defg""aadd"));
  138.         System.out.println("检查一字符串是否包含另一字符串.");
  139.         System.out.println(StringUtils.contains("defg""ef"));
  140.         System.out.println(StringUtils.containsOnly("ef""defg"));
  141.         System.out.println("返回可以处理null的toString().");
  142.         System.out.println(StringUtils.defaultString("aaaa"));
  143.         System.out.println("?" + StringUtils.defaultString(null) + "!");
  144.         System.out.println("去除字符中的空格.");
  145.         System.out.println(StringUtils.deleteWhitespace("aa  bb  cc"));
  146.         System.out.println("判断是否是某类字符.");
  147.         System.out.println(StringUtils.isAlpha("ab"));
  148.         System.out.println(StringUtils.isAlphanumeric("12"));
  149.         System.out.println(StringUtils.isBlank(""));
  150.         System.out.println(StringUtils.isNumeric("123"));
  151.     }
  152.     public void systemUtilsDemo() {
  153.         System.out.println(genHeader("SystemUtilsDemo"));
  154.         System.out.println("获得系统文件分隔符.");
  155.         System.out.println(SystemUtils.FILE_SEPARATOR);
  156.         System.out.println("获得源文件编码.");
  157.         System.out.println(SystemUtils.FILE_ENCODING);
  158.         System.out.println("获得ext目录.");
  159.         System.out.println(SystemUtils.JAVA_EXT_DIRS);
  160.         System.out.println("获得java版本.");
  161.         System.out.println(SystemUtils.JAVA_VM_VERSION);
  162.         System.out.println("获得java厂商.");
  163.         System.out.println(SystemUtils.JAVA_VENDOR);
  164.     }
  165.     public void classUtilsDemo() {
  166.         System.out.println(genHeader("ClassUtilsDemo"));
  167.         System.out.println("获取类实现的所有接口.");
  168.         System.out.println(ClassUtils.getAllInterfaces(Date.class));
  169.         System.out.println("获取类所有父类.");
  170.         System.out.println(ClassUtils.getAllSuperclasses(Date.class));
  171.         System.out.println("获取简单类名.");
  172.         System.out.println(ClassUtils.getShortClassName(Date.class));
  173.         System.out.println("获取包名.");
  174.         System.out.println(ClassUtils.getPackageName(Date.class));
  175.         System.out.println("判断是否可以转型.");
  176.         System.out.println(ClassUtils.isAssignable(Date.class, Object.class));
  177.         System.out.println(ClassUtils.isAssignable(Object.class, Date.class));
  178.     }
  179.     public void stringEscapeUtilsDemo() {
  180.         System.out.println(genHeader("StringEcsapeUtils"));
  181.         System.out.println("转换特殊字符.");
  182.         System.out.println("html:" + StringEscapeUtils.escapeHtml(" "));
  183.         System.out.println("html:" + StringEscapeUtils.unescapeHtml("<p>"));
  184.     }
  185.     private final class BuildDemo {
  186.         String name;
  187.         int age;
  188.         public BuildDemo(String name, int age) {
  189.             this.name = name;
  190.             this.age = age;
  191.         }
  192.         public String toString() {
  193.             ToStringBuilder tsb = new ToStringBuilder(this,
  194.                     ToStringStyle.MULTI_LINE_STYLE);
  195.             tsb.append("Name", name);
  196.             tsb.append("Age", age);
  197.             return tsb.toString();
  198.         }
  199.         public int hashCode() {
  200.             HashCodeBuilder hcb = new HashCodeBuilder();
  201.             hcb.append(name);
  202.             hcb.append(age);
  203.             return hcb.hashCode();
  204.         }
  205.         public boolean equals(Object obj) {
  206.             if (!(obj instanceof BuildDemo)) {
  207.                 return false;
  208.             }
  209.             BuildDemo bd = (BuildDemo) obj;
  210.             EqualsBuilder eb = new EqualsBuilder();
  211.             eb.append(name, bd.name);
  212.             eb.append(age, bd.age);
  213.             return eb.isEquals();
  214.         }
  215.     }
  216.     public void builderDemo() {
  217.         System.out.println(genHeader("BuilderDemo"));
  218.         BuildDemo obj1 = new BuildDemo("a"1);
  219.         BuildDemo obj2 = new BuildDemo("b"2);
  220.         BuildDemo obj3 = new BuildDemo("a"1);
  221.         System.out.println("toString()");
  222.         System.out.println(obj1);
  223.         System.out.println(obj2);
  224.         System.out.println(obj3);
  225.         System.out.println("hashCode()");
  226.         System.out.println(obj1.hashCode());
  227.         System.out.println(obj2.hashCode());
  228.         System.out.println(obj3.hashCode());
  229.         System.out.println("equals()");
  230.         System.out.println(obj1.equals(obj2));
  231.         System.out.println(obj1.equals(obj3));
  232.     }
  233.     public void numberUtils() {
  234.         System.out.println(genHeader("NumberUtils"));
  235.         System.out.println("字符串转为数字(不知道有什么用).");
  236.         System.out.println(NumberUtils.stringToInt("ba"33));
  237.         System.out.println("从数组中选出最大值.");
  238.         System.out.println(NumberUtils.max(new int[] { 1234 }));
  239.         System.out.println("判断字符串是否全是整数.");
  240.         System.out.println(NumberUtils.isDigits("123.1"));
  241.         System.out.println("判断字符串是否是有效数字.");
  242.         System.out.println(NumberUtils.isNumber("0123.1"));
  243.     }
  244.     public void dateFormatUtilsDemo() {
  245.         System.out.println(genHeader("DateFormatUtilsDemo"));
  246.         System.out.println("格式化日期输出.");
  247.         System.out.println(DateFormatUtils.format(System.currentTimeMillis(),
  248.                 "yyyy-MM-dd HH:mm:ss"));
  249.         System.out.println("秒表.");
  250.         StopWatch sw = new StopWatch();
  251.         sw.start();
  252.         for (Iterator iterator = DateUtils.iterator(new Date(),
  253.                 DateUtils.RANGE_WEEK_CENTER); iterator.hasNext();) {
  254.             Calendar cal = (Calendar) iterator.next();
  255.             System.out.println(DateFormatUtils.format(cal.getTime(),
  256.                     "yy-MM-dd HH:mm"));
  257.         }
  258.         sw.stop();
  259.         System.out.println("秒表计时:" + sw.getTime());
  260.     }
  261.     private String genHeader(String head) {
  262.         String[] header = new String[3];
  263.         header[0] = StringUtils.repeat("*"50);
  264.         header[1] = StringUtils.center("  " + head + "  "50"^O^");
  265.         header[2] = header[0];
  266.         return StringUtils.join(header, "/n");
  267.     }
  268.     /**
  269.      * @param args
  270.      */
  271.     public static void main(String[] args) {
  272.         LangDemo langDemo = new LangDemo();
  273.         langDemo.charSetDemo();
  274.         langDemo.charSetUtilsDemo();
  275.         langDemo.objectUtilsDemo();
  276.         langDemo.serializationUtilsDemo();
  277.         langDemo.randomStringUtilsDemo();
  278.         langDemo.stringUtilsDemo();
  279.         langDemo.systemUtilsDemo();
  280.         langDemo.classUtilsDemo();
  281.         langDemo.stringEscapeUtilsDemo();
  282.         langDemo.builderDemo();
  283.         langDemo.numberUtils();
  284.         langDemo.dateFormatUtilsDemo();
  285.     }
  286. }
相关文章
|
12月前
|
Apache
java.lang.NoClassDefFoundError: org/apache/commons/io/output/UnsynchronizedByteArrayOutputStream
java.lang.NoClassDefFoundError: org/apache/commons/io/output/UnsynchronizedByteArrayOutputStream
526 0
|
Java Apache Maven
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory解决方法
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory解决方法
1333 0
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory解决方法
|
5月前
|
Java 数据库连接 Apache
深入理解Apache Commons Pool2池化技术
深入理解Apache Commons Pool2池化技术
|
6月前
|
算法 Java Apache
Apache Commons
Apache Commons是一个开源项目,提供了一系列的工具和库,用于简化Java开发中的常见任务。
61 1
|
移动开发 前端开发 Java
Spring MVC-09循序渐进之文件上传(基于Apache Commons FileUpload)
Spring MVC-09循序渐进之文件上传(基于Apache Commons FileUpload)
100 0
|
Java 应用服务中间件 Apache
nested exception is java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntim
nested exception is java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntim
360 0
|
Java Apache
java.lang.NoClassDefFoundError:org/apache/commons/logging/LogFactory
java.lang.NoClassDefFoundError:org/apache/commons/logging/LogFactory
103 0
|
存储 Java Apache
java积累——apache commons fileupload 实现文件上传
java积累——apache commons fileupload 实现文件上传
363 0
|
Apache
Apache Commons DbUtils工具使用
Apache Commons DbUtils工具使用
149 0
Apache Commons DbUtils工具使用
|
存储 JSON Java
【小家java】Java实用数据结构Pair、MutablePair、ImmutablePair详解(推荐apache的commons组件提供)
【小家java】Java实用数据结构Pair、MutablePair、ImmutablePair详解(推荐apache的commons组件提供)

推荐镜像

更多