Java工具類之Apache的Commons Lang和BeanUtils
Apache Commons包估計是Java中使用最廣發的工具包了,很多框架都依賴於這組工具包中的一部分,它提供了我們常用的一些程式設計需要,但是JDK沒能提供的機能,最大化的減少重複程式碼的編寫。
http://commons.apache.org/
1)Commons Lang是對JDK中java.lang包的補充,提供了各種各樣的Utilities工具類,這裡說說最常用的幾個工具類。
版本:commons-lang3-3.1.jar
1、字串的空判斷
2、字串的Trim
3、字串的分割
4、字串的連線
5、字串的Escape
6、隨機數
7、陣列
8、日期
2)Commons Beanutils是一組專門用於操作Bean的工具類,目前很多流行的框架基本都離不開他。
版本:commons-beanutils-1.8.3.jar
1、獲取欄位值
2、設定欄位值
3、賦值Bean
4、Bean的describe
5、Bean的populate
6、獲取Bean的陣列集合欄位值
7、獲取Bean的Map欄位值
8、獲取Bean的巢狀欄位值
9、URL欄位的特殊處理
10、日期的轉化
Jodd的BeanUtil也提供了相當方便的操作Bean的API。
Apache Commons IO入門教程:http://www.importnew.com/13715.html
http://commons.apache.org/
1)Commons Lang是對JDK中java.lang包的補充,提供了各種各樣的Utilities工具類,這裡說說最常用的幾個工具類。
版本:commons-lang3-3.1.jar
1、字串的空判斷
- //isEmpty
- System.out.println(StringUtils.isEmpty(null)); // true
- System.out.println(StringUtils.isEmpty("")); // true
- System.out.println(StringUtils.isEmpty(" ")); // false
- System.out.println(StringUtils.isEmpty("bob")); // false
- System.out.println(StringUtils.isEmpty(" bob ")); // false
- //isBlank
- System.out.println(StringUtils.isBlank(null)); // true
- System.out.println(StringUtils.isBlank("")); // true
- System.out.println(StringUtils.isBlank(" ")); // true
- System.out.println(StringUtils.isBlank("bob")); // false
- System.out.println(StringUtils.isBlank(" bob ")); // false
2、字串的Trim
- //trim
- System.out.println(StringUtils.trim(null)); // null
- System.out.println(StringUtils.trim("")); // ""
- System.out.println(StringUtils.trim(" ")); // ""
- System.out.println(StringUtils.trim("abc")); // "abc"
- System.out.println(StringUtils.trim(" abc")); // "abc"
- System.out.println(StringUtils.trim(" abc ")); // "abc"
- System.out.println(StringUtils.trim(" ab c ")); // "ab c"
- //strip
- System.out.println(StringUtils.strip(null)); // null
- System.out.println(StringUtils.strip("")); // ""
- System.out.println(StringUtils.strip(" ")); // ""
- System.out.println(StringUtils.strip("abc")); // "abc"
- System.out.println(StringUtils.strip(" abc")); // "abc"
- System.out.println(StringUtils.strip("abc ")); // "abc"
- System.out.println(StringUtils.strip(" abc ")); // "abc"
- System.out.println(StringUtils.strip(" ab c ")); // "ab c"
- System.out.println(StringUtils.strip(" abcyx", "xyz")); // " abc"
- System.out.println(StringUtils.stripStart("yxabcxyz ", "xyz")); // "abcxyz "
- System.out.println(StringUtils.stripEnd(" xyzabcyx", "xyz")); // " xyzabc"
3、字串的分割
- //預設半形空格分割
- String str1 = "aaa bbb ccc";
- String[] dim1 = StringUtils.split(str1); // => ["aaa", "bbb", "ccc"]
- System.out.println(dim1.length);//3
- System.out.println(dim1[0]);//"aaa"
- System.out.println(dim1[1]);//"bbb"
- System.out.println(dim1[2]);//"ccc"
- //指定分隔符
- String str2 = "aaa,bbb,ccc";
- String[] dim2 = StringUtils.split(str2, ","); // => ["aaa", "bbb", "ccc"]
- System.out.println(dim2.length);//3
- System.out.println(dim2[0]);//"aaa"
- System.out.println(dim2[1]);//"bbb"
- System.out.println(dim2[2]);//"ccc"
- //去除空字串
- String str3 = "aaa,,bbb";
- String[] dim3 = StringUtils.split(str3, ","); // => ["aaa", "bbb"]
- System.out.println(dim3.length);//2
- System.out.println(dim3[0]);//"aaa"
- System.out.println(dim3[1]);//"bbb"
- //包含空字串
- String str4 = "aaa,,bbb";
- String[] dim4 = StringUtils.splitPreserveAllTokens(str4, ","); // => ["aaa", "", "bbb"]
- System.out.println(dim4.length);//3
- System.out.println(dim4[0]);//"aaa"
- System.out.println(dim4[1]);//""
- System.out.println(dim4[2]);//"bbb"
- //指定分割的最大次數(超過後不分割)
- String str5 = "aaa,bbb,ccc";
- String[] dim5 = StringUtils.split(str5, ",", 2); // => ["aaa", "bbb,ccc"]
- System.out.println(dim5.length);//2
- System.out.println(dim5[0]);//"aaa"
- System.out.println(dim5[1]);//"bbb,ccc"
4、字串的連線
- //陣列元素拼接
- String[] array = {"aaa", "bbb", "ccc"};
- String result1 = StringUtils.join(array, ",");
- System.out.println(result1);//"aaa,bbb,ccc"
- //集合元素拼接
- List<String> list = new ArrayList<String>();
- list.add("aaa");
- list.add("bbb");
- list.add("ccc");
- String result2 = StringUtils.join(list, ",");
- System.out.println(result2);//"aaa,bbb,ccc"
5、字串的Escape
- System.out.println(StringEscapeUtils.escapeCsv("測試測試哦"));//"測試測試哦"
- System.out.println(StringEscapeUtils.escapeCsv("測試,測試哦"));//"\"測試,測試哦\""
- System.out.println(StringEscapeUtils.escapeCsv("測試\n測試哦"));//"\"測試\n測試哦\""
- System.out.println(StringEscapeUtils.escapeHtml4("測試測試哦
- "));//"<p>測試測試哦</p>"
- System.out.println(StringEscapeUtils.escapeJava("\"rensaninng\",歡迎您!"));//"\"rensaninng\"\uFF0C\u6B22\u8FCE\u60A8\uFF01"
- System.out.println(StringEscapeUtils.escapeEcmaScript("測試'測試哦"));//"\u6D4B\u8BD5\'\u6D4B\u8BD5\u54E6"
- System.out.println(StringEscapeUtils.escapeXml("<tt>\"bread\" & \"butter\"</tt>"));//"<tt>"bread" & "butter"</tt>"
6、隨機數
- // 10位英字
- System.out.println(RandomStringUtils.randomAlphabetic(10));
- // 10位英數
- System.out.println(RandomStringUtils.randomAlphanumeric(10));
- // 10位ASCII碼
- System.out.println(RandomStringUtils.randomAscii(10));
- // 指定文字10位
- System.out.println(RandomStringUtils.random(10, "abcde"));
7、陣列
- // 追加元素到陣列尾部
- int[] array1 = {1, 2};
- array1 = ArrayUtils.add(array1, 3); // => [1, 2, 3]
- System.out.println(array1.length);//3
- System.out.println(array1[2]);//3
- // 刪除指定位置的元素
- int[] array2 = {1, 2, 3};
- array2 = ArrayUtils.remove(array2, 2); // => [1, 2]
- System.out.println(array2.length);//2
- // 擷取部分元素
- int[] array3 = {1, 2, 3, 4};
- array3 = ArrayUtils.subarray(array3, 1, 3); // => [2, 3]
- System.out.println(array3.length);//2
- // 陣列拷貝
- String[] array4 = {"aaa", "bbb", "ccc"};
- String[] copied = (String[]) ArrayUtils.clone(array4); // => {"aaa", "bbb", "ccc"}
- System.out.println(copied.length);//3
- // 判斷是否包含某元素
- String[] array5 = {"aaa", "bbb", "ccc", "bbb"};
- boolean result1 = ArrayUtils.contains(array5, "bbb"); // => true
- System.out.println(result1);//true
- // 判斷某元素在陣列中出現的位置(從前往後,沒有返回-1)
- int result2 = ArrayUtils.indexOf(array5, "bbb"); // => 1
- System.out.println(result2);//1
- // 判斷某元素在陣列中出現的位置(從後往前,沒有返回-1)
- int result3 = ArrayUtils.lastIndexOf(array5, "bbb"); // => 3
- System.out.println(result3);//3
- // 陣列轉Map
- Map<Object, Object> map = ArrayUtils.toMap(new String[][]{
- {"key1", "value1"},
- {"key2", "value2"}
- });
- System.out.println(map.get("key1"));//"value1"
- System.out.println(map.get("key2"));//"value2"
- // 判斷陣列是否為空
- Object[] array61 = new Object[0];
- Object[] array62 = null;
- Object[] array63 = new Object[]{"aaa"};
- System.out.println(ArrayUtils.isEmpty(array61));//true
- System.out.println(ArrayUtils.isEmpty(array62));//true
- System.out.println(ArrayUtils.isNotEmpty(array63));//true
- // 判斷陣列長度是否相等
- Object[] array71 = new Object[]{"aa", "bb", "cc"};
- Object[] array72 = new Object[]{"dd", "ee", "ff"};
- System.out.println(ArrayUtils.isSameLength(array71, array72));//true
- // 判斷陣列元素內容是否相等
- Object[] array81 = new Object[]{"aa", "bb", "cc"};
- Object[] array82 = new Object[]{"aa", "bb", "cc"};
- System.out.println(ArrayUtils.isEquals(array81, array82));
- // Integer[] 轉化為 int[]
- Integer[] array9 = new Integer[]{1, 2};
- int[] result = ArrayUtils.toPrimitive(array9);
- System.out.println(result.length);//2
- System.out.println(result[0]);//1
- // int[] 轉化為 Integer[]
- int[] array10 = new int[]{1, 2};
- Integer[] result10 = ArrayUtils.toObject(array10);
- System.out.println(result.length);//2
- System.out.println(result10[0].intValue());//1
8、日期
- // 生成Date物件
- Date date = DateUtils.parseDate("2010/01/01 11:22:33", new String[]{"yyyy/MM/dd HH:mm:ss"});
- // 10天后
- Date tenDaysAfter = DateUtils.addDays(date, 10); // => 2010/01/11 11:22:33
- System.out.println(DateFormatUtils.format(tenDaysAfter, "yyyy/MM/dd HH:mm:ss"));
- // 前一個月
- Date prevMonth = DateUtils.addMonths(date, -1); // => 2009/12/01 11:22:33
- System.out.println(DateFormatUtils.format(prevMonth, "yyyy/MM/dd HH:mm:ss"));
- // 判斷是否是同一天
- Date date1 = DateUtils.parseDate("2010/01/01 11:22:33", new String[]{"yyyy/MM/dd HH:mm:ss"});
- Date date2 = DateUtils.parseDate("2010/01/01 22:33:44", new String[]{"yyyy/MM/dd HH:mm:ss"});
- System.out.println(DateUtils.isSameDay(date1, date2));// true
- // 日期格式化
- System.out.println(DateFormatUtils.format(new Date(), "yyyy/MM/dd HH:mm:ss"));
2)Commons Beanutils是一組專門用於操作Bean的工具類,目前很多流行的框架基本都離不開他。
版本:commons-beanutils-1.8.3.jar
1、獲取欄位值
- SampleBean bean1 = new SampleBean();
- bean1.setName("rensanning");
- bean1.setAge(31);
- String name = BeanUtils.getProperty(bean1, "name");
- String age = BeanUtils.getProperty(bean1, "age");
- System.out.println(name);
- System.out.println(age);
2、設定欄位值
- SampleBean bean2 = new SampleBean();
- BeanUtils.setProperty(bean2, "name", "rensanning");
- BeanUtils.setProperty(bean2, "age", 31);
- System.out.println(bean2.getName());
- System.out.println(bean2.getAge());
3、賦值Bean
- SampleBean bean3 = new SampleBean();
- bean3.setName("rensanning");
- bean3.setAge(31);
- SampleBean clone = (SampleBean) BeanUtils.cloneBean(bean3);
- System.out.println(clone.getName());
- System.out.println(clone.getAge());
4、Bean的describe
- SampleBean bean4 = new SampleBean();
- bean4.setName("rensanning");
- bean4.setAge(31);
- @SuppressWarnings("unchecked")
- Map<String, String> map4 = BeanUtils.describe(bean4);
- System.out.println(map4.get("name"));
- System.out.println(map4.get("age"));
5、Bean的populate
- SampleBean bean5 = new SampleBean();
- Map<String, String> map5 = new HashMap<String, String>();
- map5.put("name", "rensanning");
- map5.put("age", "31");
- BeanUtils.populate(bean5, map5);
- System.out.println(bean5.getName());
- System.out.println(bean5.getAge());
6、獲取Bean的陣列集合欄位值
- SampleBean bean6 = new SampleBean();
- bean6.setArray(new String[]{"a", "b", "c"});
- List<String> list0 = new ArrayList<String>();
- list0.add("d");
- list0.add("e");
- list0.add("f");
- bean6.setList(list0);
- String[] array = BeanUtils.getArrayProperty(bean6, "array");
- System.out.println(array.length);//3
- System.out.println(array[0]);//"a"
- System.out.println(array[1]);//"b"
- System.out.println(array[2]);//"c"
- String[] list = BeanUtils.getArrayProperty(bean6, "list");
- System.out.println(list.length);//3
- System.out.println(list[0]);//"d"
- System.out.println(list[1]);//"e"
- System.out.println(list[2]);//"f"
- System.out.println(BeanUtils.getProperty(bean6, "array[1]"));//"b"
- System.out.println(BeanUtils.getIndexedProperty(bean6, "array", 2));//"c"
7、獲取Bean的Map欄位值
- SampleBean bean7 = new SampleBean();
- Map<String, String> map = new HashMap<String, String>();
- map.put("key1", "value1");
- map.put("key2", "value2");
- bean7.setMap(map);
- String value1 = BeanUtils.getMappedProperty(bean7, "map", "key1");
- System.out.println(value1);//"value1"
- String value2 = BeanUtils.getMappedProperty(bean7, "map", "key2");
- System.out.println(value2);//"value2"
- System.out.println(BeanUtils.getProperty(bean7, "map.key1"));//"value1"
- System.out.println(BeanUtils.getProperty(bean7, "map.key2"));//"value2"
8、獲取Bean的巢狀欄位值
- SampleBean bean = new SampleBean();
- NestedBean nestedBean = new NestedBean();
- nestedBean.setNestedProperty("xxx");
- bean.setNestedBean(nestedBean);
- String value = BeanUtils.getNestedProperty(bean, "nestedBean.nestedProperty");
- System.out.println(value);//"xxx"
- System.out.println(BeanUtils.getProperty(bean, "nestedBean.nestedProperty"));//"xxx"
9、URL欄位的特殊處理
- SampleBean bean8 = new SampleBean();
- BeanUtils.setProperty(bean8, "url", "http://www.google.com/");
- URL url = bean8.getUrl();
- System.out.println(url.getProtocol());//"http"
- System.out.println(url.getHost());//"www.google.com"
- System.out.println(url.getPath());//"/"
10、日期的轉化
- SampleBean bean9 = new SampleBean();
- DateConverter converter = new DateConverter();
- converter.setPattern("yyyy/MM/dd HH:mm:ss");
- ConvertUtils.register(converter, Date.class);
- ConvertUtils.register(converter, String.class);
- BeanUtils.setProperty(bean9, "date", "2010/12/19 23:40:00");
- String value9 = BeanUtils.getProperty(bean9, "date");
- System.out.println(value9);//"2010/12/19 23:40:00"
Jodd的BeanUtil也提供了相當方便的操作Bean的API。
Apache Commons IO入門教程:http://www.importnew.com/13715.html
相關文章
- Java:org.apache.commons.beanutils.BeanUtils拷貝物件屬性JavaApacheBean物件
- org.apache.commons.lang.exception包的ExceptionUtils工具類獲取getFullStackTraceApacheException
- java程式設計之:org.apache.commons.lang3.text.StrTokenizerJava程式設計Apache
- java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactoryJavaExceptionApache
- commons-lang包常用工具類的基本使用
- [Java SE] 基礎工具庫 : Apache Commons IOJavaApache
- [轉]Apache Commons工具集Apache
- Commons-Beanutils利用鏈分析Bean
- Java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 解決方案JavaErrorApache
- Apache Commons 工具集使用簡介Apache
- Apache Commons 系列簡介 之 PoolApache
- 使用commons-beanutils迭代獲取javabean的屬性BeanJava
- java apache commons HttpClient傳送get和post請求的學習整理JavaApacheHTTPclient
- java lang包下Character類Java
- java beanUtils框架JavaBean框架
- 丟棄掉那些BeanUtils工具類吧,MapStruct真香!!!BeanStruct
- Java之Collections工具類Java
- 理解java.lang.Class類Java
- java工具類之編碼轉換工具類Java
- JAVA SSH與Class 'org.apache.commons.dbcp.BasicDataSource' not foundJavaApache
- 遭遇java.lang.NoClassDefFoundError:org/apache/tomcat/PeriodicEventListenerJavaErrorApacheTomcat
- java.lang.NoClassDefFoundError: org/apache/ibatis/session/SqlSessionFactoryJavaErrorApacheBATSessionSQL
- Apache Commons IO入門教程Apache
- java 所有的集合類以及Apache的集合類???JavaApache
- 談談Apache Jakarta Commons 下元件的學習和運用Apache元件
- Java實體對映工具MapStruct 與BeanUtils效能比較JavaStructBean
- java.lang.ClassCastException: org.apache.struts.taglib.bean.CookieTeiJavaASTExceptionApacheBeanCookie
- nested exception is java.lang.NoSuchMethodException: 實體類.()ExceptionJava
- 深入研究java.lang.ThreadLocal類Javathread
- 深入研究java.lang.Object類薦JavaObject
- 深入研究java.lang.ProcessBuilder類薦JavaUI
- Cause: java.lang.ClassCastException: java.lang.String cannot be cast to org.apache.ibatis.mapping.MappedStatementJavaASTExceptionApacheBATAPP
- 使用Apache commons email傳送郵件ApacheAI
- Apache Commons Collections反序列化漏洞Apache
- java各種commons工具包的免費下載地址Java
- 2020-10-18 BeanUtils工具類,簡化資料封裝Bean封裝
- java.lang.NoSuchMethodError: org.apache.hadoop.hbase.client.HTable.<init>JavaErrorApacheHadoopclient
- Java之繼承和抽象類Java繼承抽象