Apache Commons 常用工具类整理
其实一直都在使用常用工具类,只是从没去整理过,今天空了把一些常用的整理一下吧
怎么使用的一看就明白,另外还有注释,最后的使用pom引入的jar包
public class ApacheCommonsTest {
/**
* 从一个entity中把属性复制进另外一个entity中
*
* @throws Exception
*/
@Test
public void testCopyNewBean() throws Exception {
StuForm form = new StuForm("lee", 18, 1, new Date(), true);
Stu stu = new Stu();
BeanUtils.copyProperties(form, stu);
System.out.println(stu.toString());
}
/**
* base64 加密解密
*
* @throws Exception
*/
@Test
public void testBase64Code() throws Exception {
String name1 = "hello, my name is lee~";
System.out.println("Before: " + name1);
String name2 = Base64.encodeBase64String(name1.getBytes());
System.out.println("After encode: " + name2);
String name3 = new String(Base64.decodeBase64(name2));
System.out.println("After decode: " + name3);
String url1 = "www.lee.com.cn";
System.out.println("URL Before: " + url1);
String url2 = Base64.encodeBase64URLSafeString(url1.getBytes());
System.out.println("URL After decode: " + url2);
String url3 = new String(Base64.decodeBase64(url2));
System.out.println("URL After decode: " + url3);
}
/**
* commons 下 collection 工具包
*
* @throws Exception
*/
@Test
public void testCollection() throws Exception {
OrderedMap<String, Object> om = new LinkedMap<String, Object>();
om.put("one", 1);
om.put("two", "2");
om.put("three", "three");
om.put("fore", 4);
om.put("five", "5");
System.out.println(om.firstKey());
System.out.println(om.nextKey("fore"));
System.out.println(om.previousKey("five"));
System.out.println("==============================");
BidiMap bm = new TreeBidiMap();
bm.put("three", "3");
bm.put("five", "isfive");
System.out.println(bm.getKey("isfive").toString());
System.out.println(bm.get("three"));
// 交换key和value
BidiMap newMap = bm.inverseBidiMap();
System.out.println(newMap.size());
System.out.println("==============================");
Bag<Object> bag = new HashBag<Object>();
bag.add("abc");
bag.add("def", 3);
bag.add("ghi", 5);
System.out.println(bag.size());
// 过滤重复元素
Set<Object> onlyU = bag.uniqueSet();
Iterator<Object> i = onlyU.iterator();
while(i.hasNext()){
Object o = i.next();
System.out.println(o.toString());
}
}
/**
* Apache Commons Configuration
*
* @throws Exception
*/
@Test
public void testConfig() throws Exception {
PropertiesConfiguration p = new PropertiesConfiguration("test.properties");
System.out.println(p.getString("boy.name"));
System.out.println(p.getInt("boy.age"));
System.out.println(p.getString("boy.birth"));
p.setHeader("##this is a new string##");
p.setProperty("new.string", "newString");
// 保存在编译后的目录中
p.save();
p.save("newP");
}
/**
* Apache Commons Lang
*
* @throws Exception
*/
@Test
public void testLang() throws Exception {
String a1[] = {"1", "2", "3"};
String a2[] = {"a", "b", "c"};
// 合并数组
String a3[] = (String[])ArrayUtils.addAll(a1, a2);
for (String s : a3) {
System.out.println(s);
}
System.out.println("==============================");
String str = "hello, my name is hanmeimei! what's your name? name";
// 出现第一个和第二个name之间的string
String s1 = StringUtils.substringBetween(str, "name");
System.out.println("s1: " + s1);
// 截取第一次出现的字符串之间的string
String s2 = StringUtils.substringBetween(str, "name", "your");
System.out.println("s2: " + s2);
// StringUtils.substringAfter(str, separator)
// StringUtils.substringBefore(str, separator)
System.out.println("==============================");
// 判断该字符串是不是为数字(0~9)组成,如果是,返回true 但该方法不识别有小数点
System.out.println(StringUtils.isNumeric("454534"));
System.out.println("==============================");
System.out.println(ClassUtils.getShortClassName(Test.class));
System.out.println(ClassUtils.getPackageName(Test.class));
System.out.println("==============================");
// 判断该字符串是不是为数字(0~9)组成,如果是,返回true 可以识别有小数点
System.out.println(NumberUtils.isNumber("12334.11"));
// 不建议使用,可以使用 Integer.valueOf("[number]")
System.out.println(NumberUtils.stringToInt("33"));
System.out.println(Integer.valueOf("33"));
// 五位的随机字母和数字
System.out.println(RandomStringUtils.randomAlphanumeric(5));
System.out.println(StringEscapeUtils.escapeHtml("<html>"));
System.out.println(StringEscapeUtils.escapeJava("String"));
// StringUtils,判断是否是空格字符
System.out.println(StringUtils.isBlank(" "));
// StringUtils.isEmpty("");
// 将数组中的内容以,分隔
System.out.println(StringUtils.join(a3, ","));
// 在右边加下字符,使之总长度为6
System.out.println(StringUtils.rightPad("abc", 6, 'T'));
// 首字母大写
System.out.println(StringUtils.capitalize("abc"));
// Deletes all whitespaces from a String 删除所有空格
System.out.println(StringUtils.deleteWhitespace(" ab c "));
// 判断是否包含这个字符
System.out.println(StringUtils.contains("abc", "ba"));
// 表示左边两个字符
System.out.println(StringUtils.left("abc", 2));
}
}
<!-- apache commons -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.10</version>
</dependency>
附上地址:https://github.com/leechenxiang/maven-apache-commons
Apache Commons 常用工具类整理的更多相关文章
- Maven基础&&Spring框架阶段常用工具类整理
常用工具类 1.密码加密工具类: package com.itheima.utils; import java.security.MessageDigest; import sun.misc.BASE ...
- javascript常用工具类整理(copy)
JavaScript常用工具类 类型 日期 数组 字符串 数字 网络请求 节点 存储 其他 1.类型 isString (o) { //是否字符串 return Object.prototype.to ...
- Maven 常用工具类整理
目录 1.Apache Commons 1.1.字符串处理 1.2.集合操作 1.3.IO操作 1.4.编解码操作 2.Google Guava 2.1.多场景使用 2.2.guava-retryin ...
- Java常用工具类整理
字符数组转String package com.sunsheen.hcc.fabric.utils; /** * 字符数组工具 * @author WangSong * */ public class ...
- org.apache.commons.httpclient工具类
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpcl ...
- org.apache.commons.httpclient工具类(封装的HttpUtil)
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java ...
- IOS开发--常用工具类收集整理(Objective-C)(持续更新)
前言:整理和收集了IOS项目开发常用的工具类,最后也给出了源码下载链接. 这些可复用的工具,一定会给你实际项目开发工作锦上添花,会给你带来大大的工作效率. 重复造轮子的事情,除却自我多练习编码之外,就 ...
- commons-lang3-3.2.jar中的常用工具类的使用
这个包中的很多工具类可以简化我们的操作,在这里简单的研究其中的几个工具类的使用. 1.StringUtils工具类 可以判断是否是空串,是否为null,默认值设置等操作: /** * StringUt ...
- commons-lang常用工具类StringEscapeUtils使用--转
https://my.oschina.net/ydsakyclguozi/blog/341496 在apache commons-lang(2.3以上版本)中为我们提供了一个方便做转义的工具类,主要是 ...
随机推荐
- Hibernate框架之关联映射入门
关联映射就是将关联关系映射到数据库里,在对象模型中就是一个或多个引用. 一:配置单向多对一关联 在Emp类中定义一个Dept属性,而在Dept类中无须定义用于存放Emp对象的集合属性 01.Dept. ...
- Windows工作集内存
Windows任务管理器默认情况下,“内存(私人工作集)”列处于选中状态. 私人工作集是工作集的一个子集,它是描述每个进程所使用的内存数量的技术术语.私人工作集专门描述了某个进程正在使用的且无法与其他 ...
- 你可以使用 play framework 做5件很爽的事情http://www.anool.net/?p=629
1.绑定HTTP参数到JAVA方法里的参数. 使用PLAY可以很简单的从JAVA代码中检索HTTP参数.只要把方法参数申明成和HTTP参数相同既可. 比如,这个request: Http代码 /art ...
- 1、Flat UI Getting started(文档翻译)
下载链接:http://www.bootcss.com/p/flat-ui/ 一.什么是Flat UI? Flat UI 是一种漂亮的Boostrap主题.我们重新设计了它的很多组件,使得其看起来扁平 ...
- 小型工厂企业网站究竟该怎么做好SEO优化,从而带来更多订单?
中 小企业以及小型工厂做好SEO工作,每年从SEO带来的订单量还是很可观的,随着互联网的蓬勃发展,越来越多的小型工厂型企业网站开始逐渐走向互联网营 销,开始逐渐利用互联网开展销售工作!但是大部分的工厂 ...
- Microsoft Dynamics CRM 前瑞开发
做CRM开发最大的感受就是其前瑞开发过程中,调试起来比较麻烦,需要做一些断点还要配制一些浏览器设置,对新手来说比较困难.还有就是对REST调试,经常为了调试一个正确的结果而花费大量的时间.现在推荐一个 ...
- Creating External Lists From Code
You can create an external list based on an entity (external content type) defined in SharePoint Bus ...
- IOS开发--常用的基本GDB命令
gdb不是万能的,可是没有gdb却是万万不能的.这里给大家简单介绍下iOS开发中最基本的gdb命令. po po是print-object的简写,可用来打印所有NSObject对象.使用举例如下: ( ...
- 关于配置并发访问的服务器apache、nginx
一. apache,nginx比较 关于Apache与Nginx的优势比较 (apache计算密集型 nginx io密集型 各有优势,不存在谁取代谁) 二.nginx 基于nginx ...
- Effective Java 73 Avoid thread groups
Thread groups were originally envisioned as a mechanism for isolating applets for security purposes. ...