在项目中我们看到了很多同事喜欢进行相关的封装,我并不是反对,但是有些方法其实第三方jar包已经为我们提供了相同
的功能,所以我们应该要熟练的运行第三方jar提供的相关类来完成相关功能,从而提高项目的开发效率。在Apache中为我们提供了一系列的常用工具类,StringUtils,BeanUtils,CollectionUtils等等,下面我们我介绍一些常用类的用法。其实这些方法大家应该都已经非常熟悉了,这里只是简单的记录下,免得以后要用的时候还要去网上查阅资料。
A.common-lang
StringUtils
public class StringUtilsExample {
// 判断字符为空
public static boolean isEmpty(String str) {
return StringUtils.isEmpty(str);
}
// 判断字符不为空
public static boolean isNotEmpty(String str) {
return StringUtils.isNotEmpty(str);
}
// 判断字符为空,包括空白字符
public static boolean isBlank(String str) {
return StringUtils.isBlank(str);
}
// 判断字符不为空,包括空白字符
public static boolean isNotBlank(String str) {
return StringUtils.isNotBlank(str);
}
// 去除字符串前后空白
public static String trim(String str) {
return StringUtils.trim(str);
}
// 去除字符前后空白,如果字符串为null,则输出null
public static String trimNotNull(String str) {
return StringUtils.trimToNull(str);
}
// 去除字符前后空白,如果字符串为null,则输出""
public static String trimNotEmpty(String str) {
return StringUtils.trimToEmpty(str);
}
// 去除字符前后空格
public static String strip(String str) {
return StringUtils.strip(str);
}
// 去除字符前后空格,如果字符串为null输出null
public static String stripToNull(String str) {
return StringUtils.stripToNull(str);
}
// 分割字符串
public static String[] spiltstr(String str, String symple) {
return StringUtils.split(str, symple);
}
}
DateFormatUtils
jdk提供的时间格式化类simpleDateFormat是一个线程不安全,而DateFormatUtils 是线程安全类。
public class DateFormatUtilsExample {
// 转换时间格式为yyyy-mm-dd
public static String getISODateFormat(Date date) {
return DateFormatUtils.ISO_DATE_FORMAT.format(date);
}
// 转换时间格式为yyyy-MM-ddZZ 带时区
public static String getISODateTimeAndTimeZoneFormat(Date date) {
return DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.format(date);
}
// 转换时间格式为 yyyy-MM-dd'T'HH:mm:ss
public static String getISODateTimeFormat(Date date) {
return DateFormatUtils.ISO_DATETIME_FORMAT.format(date);
}
// 转换时间格式为 yyyy-MM-dd'T'HH:mm:ss
public static String getISODateTimeZone(Date date) {
return DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(date);
}
// 时间格式为 'T'HH:mm:ss
public static String getIOSTimeFormat(Date date) {
return DateFormatUtils.ISO_TIME_FORMAT.format(date);
}
// 时间格式为 HH:mm:ss
public static String getIOSTimeNotTFormat(Date date) {
return DateFormatUtils.ISO_TIME_NO_T_FORMAT.format(date);
}
// 时间格式为 HH:mm:ssZZ
public static String getIOSTimeAndTimeZone(Date date) {
return DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.format(date);
}
// 'T'HH:mm:ssZZ
public static String getIOSTimeNotTimeZoneFormat(Date date) {
return DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.format(date);
}
// //自定义时间格式
public static String getTimeFormat(Calendar calendar, String pattern) {
return DateFormatUtils.format(calendar, pattern);
}
// 自定义时间格式
public static String getTimeFormat(Date date, String pattern) {
return DateFormatUtils.format(date, pattern);
}
// 自定义时间格式
public static String getTimeFormat(Long time, String pattern) {
return DateFormatUtils.format(time, pattern);
}
// 将date类型转换成字符串
public static Date parse(String str,String[] parsePatterns) throws ParseException {
return DateUtils.parseDate(str, parsePatterns);
}
ArrayUtils
public class ArrayUtilsExample {
// 判断两个集合是否相等
public static boolean isEquals(Object array1, Object array2) {
return ArrayUtils.isEquals(array1, array2);
}
// 判断数组为空
public static boolean isEmpty(Object[] array) {
return ArrayUtils.isEmpty(array);
}
// 将二维数组转换成map集合
@SuppressWarnings("rawtypes")
public static Map toMap(Object[] array) {
return ArrayUtils.toMap(array);
}
// 数组中是否包含该对象
public static boolean contains(Object[] array, Object objectToFind) {
return ArrayUtils.contains(array, objectToFind);
}
// 数组中添加元素
public static Object[] add(Object[] array, Object element) {
return ArrayUtils.add(array, element);
}
//将集合array2中的元素全部添加到集合arrays1中
public static Object[] addAll(Object[] array1, Object[] array2) {
return ArrayUtils.addAll(array1, array2);
}
//集合中移除元素,根据索引
public static Object[] remove(Object[] array, int index) {
return ArrayUtils.remove(array, index);
}
//集合中移除元素,根据对象名称
public static Object[] removeElement(Object[] array, Object element) {
return ArrayUtils.removeElement(array, element);
}
//将数组集合进行反转
public static void reverse(Object[] array) {
ArrayUtils.reverse(array);
}
@SuppressWarnings("unchecked")
}
B.common-beautils
BeanUtils
// 将对象source的属性值拷贝到对象target对象中
public static void copyProperties(Object target, Object source)
throws IllegalAccessException, InvocationTargetException {
BeanUtils.copyProperties(target, source);
}
// 将对象转换成map集合
@SuppressWarnings("rawtypes")
public static Map describe(Object bean) throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
return BeanUtils.describe(bean);
}
// 设置对象的字段的属性值
public static void setPorperties(Object bean, String fieldName,
String fieldValue) throws IllegalAccessException,
InvocationTargetException {
BeanUtils.setProperty(bean, fieldName, fieldValue);
}
// 获取属性的值
public static String getProperties(Object bean, String filedName)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
return BeanUtils.getProperty(bean, filedName);
}
// 将属性值为value拷贝到对象bean字段为name中
public static void copyProperty(Object bean, String name, String value)
throws IllegalAccessException, InvocationTargetException {
BeanUtils.copyProperty(bean, name, value);
}
// 将map集合转换成对象
@SuppressWarnings("rawtypes")
public static void populate(Object bean, Map map)
throws IllegalAccessException, InvocationTargetException {
BeanUtils.populate(bean, map);
}
ConstructorUtils
//通过构造函数获取对象
public static Object invokeConstructor(Class<?> clazz, Object[] args)
throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException, InstantiationException {
return ConstructorUtils.invokeConstructor(clazz, args);
}
//通过构造函数获取对象 ,不过该方法对参数的要求比较的严格,所传入的class类型都为基本数据类型
public static Object invokeExactConstructor(Class<?> clazz, Object[] args,
Class<?>[] argTypes) throws NoSuchMethodException,
IllegalAccessException, InvocationTargetException,
InstantiationException {
return ConstructorUtils.invokeExactConstructor(clazz, args, argTypes);
}
MethodUtils
// 执行对象中的某个方法,如果该方法没有返回值,则返回为null
public static Object invokeMethod(Object object, String methodName,
Object[] arg) throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException {
return MethodUtils.invokeMethod(object, methodName, arg);
}
public static Object invokeExamMethod(Object object, String methodName,
Object[] arg, Class<?>[] clazz) throws NoSuchMethodException,
IllegalAccessException, InvocationTargetException {
return MethodUtils.invokeExactMethod(object, methodName, arg, clazz);
}
common-collection
CollectionUtils
// 集合并集
public static Collection<?> union(Collection<?> a, Collection<?> b) {
return CollectionUtils.union(a, b);
}
// 集合交集
public static Collection<?> intersection(Collection<?> a, Collection<?> b) {
return CollectionUtils.intersection(a, b);
}
// 交集的补集
public static Collection<?> disjunction(Collection<?> a, Collection<?> b) {
return CollectionUtils.disjunction(a, b);
}
// 集合相减
public static Collection<?> subtract(Collection<?> a, Collection<?> b) {
return CollectionUtils.subtract(a, b);
}
C. common-io
FileUtils
//读取文件,将其转换成字节数组
public static byte[] readFileToByteArray(File file) throws IOException {
return FileUtils.readFileToByteArray(file);
}
//读取文件,将其转换成字符串
public static String readFileToString(File file) throws IOException {
return FileUtils.readFileToString(file);
}
//该方法是用data内容替换了file文件中原来的内容
public static void writeStringToFile(File file, String data)
throws IOException {
FileUtils.writeStringToFile(file, data);
}
//将文件srcFile拷贝到desFile中
public static void copyFile(File srcFile, File desFile) throws IOException {
FileUtils.copyFile(srcFile, desFile);
}
//将文件拷贝到目录下
public static void copyFileToDirectory(File srcDir, File destDir)
throws IOException {
FileUtils.copyDirectory(srcDir, destDir);
}
//拷贝URL类型
public static void copyURLToFile(URL source, File destination)
throws IOException {
FileUtils.copyURLToFile(source, destination);
}
//删除该目录下的所有文件
public static void delDirectory(File directory) throws IOException {
FileUtils.deleteDirectory(directory);
}
//清除该目录下的所有文件,但目录不删除
public static void cleanDirectory(File directory) throws IOException {
FileUtils.cleanDirectory(directory);
}
IOUtils
// 读取流信息
public static List<String> readLines(InputStream input) throws IOException {
return IOUtils.readLines(input);
}
// 读取流信息
public static List<String> readLines(Reader reader) throws IOException {
return IOUtils.readLines(reader);
}
// 批量写入文件中,并覆盖原有的文本
public static void writeLines(List<String> lines, File file)
throws FileNotFoundException, IOException {
IOUtils.writeLines(lines, null, new FileOutputStream(file));
}
//批量写入文件中,并在后面追加
public static void writeOneLine(List<String> lines, File file)
throws IOException {
OutputStream outputStream = new FileOutputStream(file, true);
IOUtils.writeLines(lines, null, outputStream, "UTF-8");
}
//将流对象转换成字符串
public static String toString(InputStream input) throws IOException {
return IOUtils.toString(input);
}
//关闭流对象
public static void closeIO(Closeable closeable) {
IOUtils.closeQuietly(closeable);
}
由于这些方法使用都非常的简单,所以相关的测试代码就不列举了,如果有不清楚的地方可以查看Apache相关的官方文档。