别再自己瞎写工具类了,SpringBoot 内置工具类应有尽有,建议收藏( 二 )

3. 路径相关工具方法
// 解析路径字符串 , 优化其中的 “..”String cleanPath(String path)// 解析路径字符串 , 解析出文件名部分String getFilename(String path)// 解析路径字符串 , 解析出文件后缀名String getFilenameExtension(String path)// 比较两个两个字符串 , 判断是否是同一个路径 。会自动处理路径中的 “..”boolean pathEquals(String path1, String path2)// 删除文件路径名中的后缀部分String stripFilenameExtension(String path)// 以 “. 作为分隔符 , 获取其最后一部分String unqualify(String qualifiedName)// 以指定字符作为分隔符 , 获取其最后一部分String unqualify(String qualifiedName, char separator)CollectionUtils1. 集合判断工具
// 判断 List/Set 是否为空boolean isEmpty(Collection<?> collection)// 判断 Map 是否为空boolean isEmpty(Map<?,?> map)// 判断 List/Set 中是否包含某个对象boolean containsInstance(Collection<?> collection, Object element)// 以迭代器的方式 , 判断 List/Set 中是否包含某个对象boolean contains(Iterator<?> iterator, Object element)// 判断 List/Set 是否包含某些对象中的任意一个boolean containsAny(Collection<?> source, Collection<?> candidates)// 判断 List/Set 中的每个元素是否唯一 。即 List/Set 中不存在重复元素boolean hasUniqueObject(Collection<?> collection) 
2. 集合操作工具
// 将 Array 中的元素都添加到 List/Set 中<E> void mergeArrayIntoCollection(Object array, Collection<E> collection)// 将 Properties 中的键值对都添加到 Map 中<K,V> void mergePropertiesIntoMap(Properties props, Map<K,V> map)// 返回 List 中最后一个元素<T> T lastElement(List<T> list)// 返回 Set 中最后一个元素<T> T lastElement(Set<T> set)// 返回参数 candidates 中第一个存在于参数 source 中的元素<E> E findFirstMatch(Collection<?> source, Collection<E> candidates)// 返回 List/Set 中指定类型的元素 。<T> T findValueOfType(Collection<?> collection, Class<T> type)// 返回 List/Set 中指定类型的元素 。如果第一种类型未找到 , 则查找第二种类型 , 以此类推Object findValueOfType(Collection<?> collection, Class<?>[] types)// 返回 List/Set 中元素的类型Class<?> findCommonElementType(Collection<?> collection)文件、资源、IO 流FileCopyUtils1. 输入
// 从文件中读入到字节数组中byte[] copyToByteArray(File in)// 从输入流中读入到字节数组中byte[] copyToByteArray(InputStream in)// 从输入流中读入到字符串中String copyToString(Reader in)2. 输出
// 从字节数组到文件void copy(byte[] in, File out)// 从文件到文件int copy(File in, File out)// 从字节数组到输出流void copy(byte[] in, OutputStream out)// 从输入流到输出流int copy(InputStream in, OutputStream out)// 从输入流到输出流int copy(Reader in, Writer out)// 从字符串到输出流void copy(String in, Writer out)ResourceUtils【别再自己瞎写工具类了,SpringBoot 内置工具类应有尽有,建议收藏】1. 从资源路径获取文件
// 判断字符串是否是一个合法的 URL 字符串 。static boolean isUrl(String resourceLocation)// 获取 URLstatic URL getURL(String resourceLocation)// 获取文件(在 JAR 包内无法正常使用 , 需要是一个独立的文件)static File getFile(String resourceLocation)2. Resource
// 文件系统资源 D:...FileSystemResource// URL 资源 , 如 file://... http://...UrlResource// 类路径下的资源 , classpth:...ClassPathResource// Web 容器上下文中的资源(jar 包、war 包)ServletContextResource// 判断资源是否存在boolean exists()// 从资源中获得 File 对象File getFile()// 从资源中获得 URI 对象URI getURI()// 从资源中获得 URI 对象URL getURL()// 获得资源的 InputStreamInputStream getInputStream()// 获得资源的描述信息String getDescription()StreamUtils
1. 输入
void copy(byte[] in, OutputStream out)int copy(InputStream in, OutputStream out)void copy(String in, Charset charset, OutputStream out)long copyRange(InputStream in, OutputStream out, long start, long end)2. 输出
byte[] copyToByteArray(InputStream in)String copyToString(InputStream in, Charset charset)// 舍弃输入流中的内容int drain(InputStream in)反射、AOPReflectionUtils1. 获取方法
// 在类中查找指定方法Method findMethod(Class<?> clazz, String name)// 同上 , 额外提供方法参数类型作查找条件Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes)// 获得类中所有方法 , 包括继承而来的Method[] getAllDeclaredMethods(Class<?> leafClass)// 在类中查找指定构造方法Constructor<T> accessibleConstructor(Class<T> clazz, Class<?>... parameterTypes)// 是否是 equals() 方法boolean isEqualsMethod(Method method)// 是否是 hashCode() 方法boolean isHashCodeMethod(Method method)// 是否是 toString() 方法boolean isToStringMethod(Method method)// 是否是从 Object 类继承而来的方法boolean isObjectMethod(Method method)// 检查一个方法是否声明抛出指定异常boolean declaresException(Method method, Class<?> exceptionType)


推荐阅读