[Java] org.pache.commons →→→→→进入此内容的聊天室

来自 , 2019-12-25, 写在 Java, 查看 146 次.
URL http://www.code666.cn/view/a113c1ec
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.util.Calendar;
  7. import java.util.Date;
  8. import java.util.Iterator;
  9.  
  10. import org.apache.commons.lang3.ArrayUtils;
  11. import org.apache.commons.lang3.CharSet;
  12. import org.apache.commons.lang3.CharSetUtils;
  13. import org.apache.commons.lang3.ClassUtils;
  14. import org.apache.commons.lang3.ObjectUtils;
  15. import org.apache.commons.lang3.RandomStringUtils;
  16. import org.apache.commons.lang3.SerializationUtils;
  17. import org.apache.commons.lang3.StringEscapeUtils;
  18. import org.apache.commons.lang3.StringUtils;
  19. import org.apache.commons.lang3.SystemUtils;
  20. import org.apache.commons.lang3.Validate;
  21. import org.apache.commons.lang3.builder.EqualsBuilder;
  22. import org.apache.commons.lang3.builder.HashCodeBuilder;
  23. import org.apache.commons.lang3.builder.ToStringBuilder;
  24. import org.apache.commons.lang3.builder.ToStringStyle;
  25. import org.apache.commons.lang3.math.NumberUtils;
  26. import org.apache.commons.lang3.text.WordUtils;
  27. import org.apache.commons.lang3.time.DateFormatUtils;
  28. import org.apache.commons.lang3.time.DateUtils;
  29. import org.apache.commons.lang3.time.StopWatch;
  30.  
  31. /*转自霸气的小码农的博客*/
  32. public class TestLangDemo {
  33.  
  34.     public void charSetDemo() {
  35.         System.out.println ("**CharSetDemo**");
  36.         CharSet charSet = CharSet.getInstance ("aeiou");
  37.         String demoStr = "The quick brown fox jumps over the lazy dog.";
  38.         int count = 0;
  39.         for (int i = 0, len = demoStr.length(); i < len; i++) {
  40.             if (charSet.contains (demoStr.charAt (i) ) ) {
  41.                 count++;
  42.             }
  43.         }
  44.         System.out.println ("count: " + count);
  45.     }
  46.  
  47.     public void charSetUtilsDemo() {
  48.         System.out.println ("**CharSetUtilsDemo**");
  49.         System.out.println ("计算字符串中包含某字符数.");
  50.         System.out.println (CharSetUtils.count (
  51.                                 "The quick brown fox jumps over the lazy dog.", "aeiou") );
  52.  
  53.         System.out.println ("删除字符串中某字符.");
  54.         System.out.println (CharSetUtils.delete (
  55.                                 "The quick brown fox jumps over the lazy dog.", "aeiou") );
  56.  
  57.         System.out.println ("保留字符串中某字符.");
  58.         System.out.println (CharSetUtils.keep (
  59.                                 "The quick brown fox jumps over the lazy dog.", "aeiou") );
  60.  
  61.         System.out.println ("合并重复的字符.");
  62.         System.out.println (CharSetUtils.squeeze ("a  bbbbbb     c dd", "b d") );
  63.     }
  64.  
  65.     public void objectUtilsDemo() {
  66.         System.out.println ("**ObjectUtilsDemo**");
  67.         System.out.println ("Object为null时,默认打印某字符.");
  68.         Object obj = null;
  69.         System.out.println (ObjectUtils.defaultIfNull (obj, "空") );
  70.  
  71.         System.out.println ("验证两个引用是否指向的Object是否相等,取决于Object的equals()方法.");
  72.         Object a = new Object();
  73.         Object b = a;
  74.         Object c = new Object();
  75.         System.out.println (ObjectUtils.equals (a, b) );
  76.         System.out.println (ObjectUtils.equals (a, c) );
  77.  
  78.         System.out.println ("用父类Object的toString()方法返回对象信息.");
  79.         Date date = new Date();
  80.         System.out.println (ObjectUtils.identityToString (date) );
  81.         System.out.println (date);
  82.  
  83.         System.out.println ("返回类本身的toString()方法结果,对象为null时,返回0长度字符串.");
  84.         System.out.println (ObjectUtils.toString (date) );
  85.         System.out.println (ObjectUtils.toString (null) );
  86.         System.out.println (date);
  87.     }
  88.  
  89.     public void serializationUtilsDemo() {
  90.         System.out.println ("*SerializationUtils**");
  91.         Date date = new Date();
  92.         byte[] bytes = SerializationUtils.serialize (date);
  93.         System.out.println (ArrayUtils.toString (bytes) );
  94.         System.out.println (date);
  95.  
  96.         Date reDate = (Date) SerializationUtils.deserialize (bytes);
  97.         System.out.println (reDate);
  98.         System.out.println (ObjectUtils.equals (date, reDate) );
  99.         System.out.println (date == reDate);
  100.  
  101.         FileOutputStream fos = null;
  102.         FileInputStream fis = null;
  103.         try {
  104.             fos = new FileOutputStream (new File ("d:/test.txt") );
  105.             fis = new FileInputStream (new File ("d:/test.txt") );
  106.             SerializationUtils.serialize (date, fos);
  107.             Date reDate2 = (Date) SerializationUtils.deserialize (fis);
  108.  
  109.             System.out.println (date.equals (reDate2) );
  110.  
  111.         } catch (FileNotFoundException e) {
  112.             e.printStackTrace();
  113.         } finally {
  114.             try {
  115.                 fos.close();
  116.                 fis.close();
  117.             } catch (IOException e) {
  118.                 e.printStackTrace();
  119.             }
  120.         }
  121.  
  122.     }
  123.  
  124.     public void randomStringUtilsDemo() {
  125.         System.out.println ("**RandomStringUtilsDemo**");
  126.         System.out.println ("生成指定长度的随机字符串,好像没什么用.");
  127.         System.out.println (RandomStringUtils.random (500) );
  128.  
  129.         System.out.println ("在指定字符串中生成长度为n的随机字符串.");
  130.         System.out.println (RandomStringUtils.random (5, "abcdefghijk") );
  131.  
  132.         System.out.println ("指定从字符或数字中生成随机字符串.");
  133.         System.out.println (RandomStringUtils.random (5, true, false) );
  134.         System.out.println (RandomStringUtils.random (5, false, true) );
  135.  
  136.     }
  137.  
  138.     public void stringUtilsDemo() {
  139.         System.out.println ("**StringUtilsDemo**");
  140.         System.out.println ("将字符串重复n次,将文字按某宽度居中,将字符串数组用某字符串连接.");
  141.         String[] header = new String[3];
  142.         header[0] = StringUtils.repeat ("*", 50);
  143.         header[1] = StringUtils.center ("  StringUtilsDemo  ", 50, "^O^");
  144.         header[2] = header[0];
  145.         String head = StringUtils.join (header, "\n");
  146.         System.out.println (head);
  147.  
  148.         System.out.println ("缩短到某长度,用...结尾.");
  149.         System.out.println (StringUtils.abbreviate (
  150.                                 "The quick brown fox jumps over the lazy dog.", 10) );
  151.         System.out.println (StringUtils.abbreviate (
  152.                                 "The quick brown fox jumps over the lazy dog.", 15, 10) );
  153.  
  154.         System.out.println ("返回两字符串不同处索引号.");
  155.         System.out.println (StringUtils.indexOfDifference ("aaabc", "aaacc") );
  156.  
  157.         System.out.println ("返回两字符串不同处开始至结束.");
  158.         System.out.println (StringUtils.difference ("aaabcde", "aaaccde") );
  159.  
  160.         System.out.println ("截去字符串为以指定字符串结尾的部分.");
  161.         System.out.println (StringUtils.chomp ("aaabcde", "de") );
  162.  
  163.         System.out.println ("检查一字符串是否为另一字符串的子集.");
  164.         System.out.println (StringUtils.containsOnly ("aad", "aadd") );
  165.  
  166.         System.out.println ("检查一字符串是否不是另一字符串的子集.");
  167.         System.out.println (StringUtils.containsNone ("defg", "aadd") );
  168.  
  169.         System.out.println ("检查一字符串是否包含另一字符串.");
  170.         System.out.println (StringUtils.contains ("defg", "ef") );
  171.         System.out.println (StringUtils.containsOnly ("ef", "defg") );
  172.  
  173.         System.out.println ("返回可以处理null的toString().");
  174.         System.out.println (StringUtils.defaultString ("aaaa") );
  175.         System.out.println ("?" + StringUtils.defaultString (null) + "!");
  176.  
  177.         System.out.println ("去除字符中的空格.");
  178.         System.out.println (StringUtils.deleteWhitespace ("aa  bb  cc") );
  179.  
  180.         System.out.println ("分隔符处理成数组.");
  181.         String[] strArray = StringUtils.split ("a,b,,c,d,null,e", ",");
  182.         System.out.println (strArray.length);
  183.         System.out.println (strArray.toString() );
  184.  
  185.         System.out.println ("判断是否是某类字符.");
  186.         System.out.println (StringUtils.isAlpha ("ab") );
  187.         System.out.println (StringUtils.isAlphanumeric ("12") );
  188.         System.out.println (StringUtils.isBlank ("") );
  189.         System.out.println (StringUtils.isNumeric ("123") );
  190.     }
  191.  
  192.     public void systemUtilsDemo() {
  193.         System.out.println (genHeader ("SystemUtilsDemo") );
  194.         System.out.println ("获得系统文件分隔符.");
  195.         System.out.println (SystemUtils.FILE_SEPARATOR);
  196.  
  197.         System.out.println ("获得源文件编码.");
  198.         System.out.println (SystemUtils.FILE_ENCODING);
  199.  
  200.         System.out.println ("获得ext目录.");
  201.         System.out.println (SystemUtils.JAVA_EXT_DIRS);
  202.  
  203.         System.out.println ("获得java版本.");
  204.         System.out.println (SystemUtils.JAVA_VM_VERSION);
  205.  
  206.         System.out.println ("获得java厂商.");
  207.         System.out.println (SystemUtils.JAVA_VENDOR);
  208.     }
  209.  
  210.     public void classUtilsDemo() {
  211.         System.out.println (genHeader ("ClassUtilsDemo") );
  212.         System.out.println ("获取类实现的所有接口.");
  213.         System.out.println (ClassUtils.getAllInterfaces (Date.class) );
  214.  
  215.         System.out.println ("获取类所有父类.");
  216.         System.out.println (ClassUtils.getAllSuperclasses (Date.class) );
  217.  
  218.         System.out.println ("获取简单类名.");
  219.         System.out.println (ClassUtils.getShortClassName (Date.class) );
  220.  
  221.         System.out.println ("获取包名.");
  222.         System.out.println (ClassUtils.getPackageName (Date.class) );
  223.  
  224.         System.out.println ("判断是否可以转型.");
  225.         System.out.println (ClassUtils.isAssignable (Date.class, Object.class) );
  226.         System.out.println (ClassUtils.isAssignable (Object.class, Date.class) );
  227.     }
  228.  
  229.     public void stringEscapeUtilsDemo() {
  230.         System.out.println (genHeader ("StringEcsapeUtils") );
  231.         System.out.println ("转换特殊字符.");
  232.         System.out.println ("html:" + StringEscapeUtils.escapeHtml3 (" ") );
  233.         System.out.println ("html:" + StringEscapeUtils.escapeHtml4 (" ") );
  234.         System.out.println ("html:" + StringEscapeUtils.unescapeHtml3 ("<p>") );
  235.         System.out.println ("html:" + StringEscapeUtils.unescapeHtml4 ("<p>") );
  236.     }
  237.  
  238.     private final class BuildDemo {
  239.         String name;
  240.         int age;
  241.  
  242.         public BuildDemo (String name, int age) {
  243.             this.name = name;
  244.             this.age = age;
  245.         }
  246.  
  247.         public String toString() {
  248.             ToStringBuilder tsb = new ToStringBuilder (this, ToStringStyle.MULTI_LINE_STYLE);
  249.             tsb.append ("Name", name);
  250.             tsb.append ("Age", age);
  251.             return tsb.toString();
  252.         }
  253.  
  254.         public int hashCode() {
  255.             HashCodeBuilder hcb = new HashCodeBuilder();
  256.             hcb.append (name);
  257.             hcb.append (age);
  258.             return hcb.hashCode();
  259.         }
  260.  
  261.         public boolean equals (Object obj) {
  262.             if (! (obj instanceof BuildDemo) ) {
  263.                 return false;
  264.             }
  265.             BuildDemo bd = (BuildDemo) obj;
  266.             EqualsBuilder eb = new EqualsBuilder();
  267.             eb.append (name, bd.name);
  268.             eb.append (age, bd.age);
  269.             return eb.isEquals();
  270.         }
  271.     }
  272.  
  273.     public void builderDemo() {
  274.         System.out.println (genHeader ("BuilderDemo") );
  275.         BuildDemo obj1 = new BuildDemo ("a", 1);
  276.         BuildDemo obj2 = new BuildDemo ("b", 2);
  277.         BuildDemo obj3 = new BuildDemo ("a", 1);
  278.  
  279.         System.out.println ("toString()");
  280.         System.out.println (obj1);
  281.         System.out.println (obj2);
  282.         System.out.println (obj3);
  283.  
  284.         System.out.println ("hashCode()");
  285.         System.out.println (obj1.hashCode() );
  286.         System.out.println (obj2.hashCode() );
  287.         System.out.println (obj3.hashCode() );
  288.  
  289.         System.out.println ("equals()");
  290.         System.out.println (obj1.equals (obj2) );
  291.         System.out.println (obj1.equals (obj3) );
  292.     }
  293.  
  294.     public void numberUtils() {
  295.         System.out.println (genHeader ("NumberUtils") );
  296.         System.out.println ("字符串转为数字(不知道有什么用).");
  297.         System.out.println (NumberUtils.toInt ("ba", 33) );
  298.  
  299.         System.out.println ("从数组中选出最大值.");
  300.         System.out.println (NumberUtils.max (new int[] { 1, 2, 3, 4 }) );
  301.  
  302.         System.out.println ("判断字符串是否全是整数.");
  303.         System.out.println (NumberUtils.isDigits ("123.1") );
  304.  
  305.         System.out.println ("判断字符串是否是有效数字.");
  306.         System.out.println (NumberUtils.isNumber ("0123.1") );
  307.     }
  308.  
  309.     public void dateFormatUtilsDemo() {
  310.         System.out.println (genHeader ("DateFormatUtilsDemo") );
  311.         System.out.println ("格式化日期输出.");
  312.         System.out.println (DateFormatUtils.format (System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss") );
  313.  
  314.         System.out.println ("秒表.");
  315.         StopWatch sw = new StopWatch();
  316.         sw.start();
  317.  
  318.         for (Iterator iterator = DateUtils.iterator (new Date(), DateUtils.RANGE_WEEK_CENTER); iterator.hasNext();) {
  319.             Calendar cal = (Calendar) iterator.next();
  320.             System.out.println (DateFormatUtils.format (cal.getTime(),
  321.                                 "yy-MM-dd HH:mm") );
  322.         }
  323.  
  324.         sw.stop();
  325.         System.out.println ("秒表计时:" + sw.getTime() );
  326.  
  327.     }
  328.  
  329.     private String genHeader (String head) {
  330.         String[] header = new String[3];
  331.         header[0] = StringUtils.repeat ("*", 50);
  332.         header[1] = StringUtils.center ("  " + head + "  ", 50, "^O^");
  333.         header[2] = header[0];
  334.         return StringUtils.join (header, "\n");
  335.     }
  336.  
  337.     private void validateDemo() {
  338.         String[] strarray = {"a", "b", "c"};
  339.         System.out.println ("验证功能");
  340.         System.out.println (Validate.notEmpty (strarray) );
  341.     }
  342.  
  343.     private void wordUtilsDemo() {
  344.         System.out.println ("单词处理功能");
  345.         String str1 = "wOrD";
  346.         String str2 = "ghj\nui\tpo";
  347.         System.out.println (WordUtils.capitalize (str1) ); //首字母大写
  348.         System.out.println (WordUtils.capitalizeFully (str1) ); //首字母大写其它字母小写
  349.         char[] ctrg = {'.'};
  350.         System.out.println (WordUtils.capitalizeFully ("i aM.fine", ctrg) ); //在规则地方转换
  351.         System.out.println (WordUtils.initials (str1) ); //获取首字母
  352.         System.out.println (WordUtils.initials ("Ben John Lee", null) ); //取每个单词的首字母
  353.         char[] ctr = {' ', '.'};
  354.         System.out.println (WordUtils.initials ("Ben J.Lee", ctr) ); //按指定规则获取首字母
  355.         System.out.println (WordUtils.swapCase (str1) ); //大小写逆转
  356.         System.out.println (WordUtils.wrap (str2, 1) ); //解析\n和\t等字符
  357.     }
  358.  
  359.     /**
  360.      * @param args
  361.      */
  362.     public static void main (String[] args) {
  363.         TestLangDemo langDemo = new TestLangDemo();
  364.  
  365.         langDemo.charSetDemo();
  366.         langDemo.charSetUtilsDemo();
  367.         langDemo.objectUtilsDemo();
  368.         langDemo.serializationUtilsDemo();
  369.         langDemo.randomStringUtilsDemo();
  370.         langDemo.stringUtilsDemo();
  371.         langDemo.systemUtilsDemo();
  372.         langDemo.classUtilsDemo();
  373.         langDemo.stringEscapeUtilsDemo();
  374.         langDemo.builderDemo();
  375.         langDemo.numberUtils();
  376.         langDemo.dateFormatUtilsDemo();
  377.         langDemo.validateDemo();
  378.         langDemo.wordUtilsDemo();
  379.     }
  380.  
  381. }

回复 "org.pache.commons"

这儿你可以回复上面这条便签

captcha