Java常用工具类
发表于更新于
字数总计:2k阅读时长:10分钟阅读量:
Java常用工具类
1.StringUtils
1 2 3 4 5 6 7
|
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency>
|
(1)isBlank() && isNotBlank
1 2 3
| StringUtils.isBlank() : 判断是否为空 StringUtils.isNotBlank() : 判断是否不为空
|
(2)isEmpty() && isNotEmpty()
1 2 3
| StringUtils.isEmpty() : 判断是否为空 StringUtils.isNotEmpty() : 判断是否不为空
|
(3)left && right && rightPad
1 2 3 4 5 6 7 8
| String str = "17513276352"; String leftNum = StringUtils.left(str, 3); String rightNum = StringUtils.right(str, 4); System.out.println(leftNum + "*****" + rightNum);
String s = StringUtils.rightPad(leftNum, 8, "*"); System.out.println(s + rightNum);
|
2.NumberUtils
(1)isDigits()
1 2 3 4 5 6 7 8 9 10 11
| String str = "123"; System.err.println("整数结果:" + NumberUtils.isDigits(str)); str = "123.1"; System.err.println("浮点数结果:" + NumberUtils.isDigits(str)); str = "+123.1"; System.err.println("带➕的浮点数:" + NumberUtils.isDigits(str)); str = "-123.1"; System.err.println("带➖号的浮点数:" + NumberUtils.isDigits(str)); str = "123.1aa"; System.err.println("字符串:" + NumberUtils.isDigits(str));
|
(2)isParsable()
1 2 3 4 5 6 7 8 9 10 11
| String str = "123"; System.err.println("整数结果:" + NumberUtils.isParsable(str)); str = "123.1"; System.err.println("浮点数结果:" + NumberUtils.isParsable(str)); str = "+123.1"; System.err.println("带➕的浮点数:" + NumberUtils.isParsable(str)); str = "-123.1"; System.err.println("带➖号的浮点数:" + NumberUtils.isParsable(str)); str = "123.1aa"; System.err.println("字符串:" + NumberUtils.isParsable(str));
|
(3)isCreatable()
1 2 3 4 5 6 7 8 9 10 11
| String str = "123"; System.err.println("整数结果:" + NumberUtils.isCreatable(str)); str = "123.1"; System.err.println("浮点数结果:" + NumberUtils.isCreatable(str)); str = "+123.1"; System.err.println("带➕的浮点数:" + NumberUtils.isCreatable(str)); str = "-123.1"; System.err.println("带➖号的浮点数:" + NumberUtils.isCreatable(str)); str = "123.1aa"; System.err.println("字符串:" + NumberUtils.isCreatable(str));
|
3.ObjectUtils
(1)firstNonNull()
1 2 3 4 5
| String str1 = null; String str2 = null; String str3 = "123"; System.err.println(ObjectUtils.firstNonNull(str1, str2, str3));
|
4.ArrayUtils
(1)isEmpty()
1 2 3 4 5 6
| String[] str = new String[0]; str = null; System.err.println(ArrayUtils.isEmpty(str)); String[] str2 = {"1", "2"}; System.err.println(ArrayUtils.isEmpty(str2));
|
(2)toString() && add()
1 2 3 4 5
| String[] str1 = new String[1]; str1[0] = "1"; String[] strings = ArrayUtils.add(str1, "2"); System.out.println(ArrayUtils.toString(strings));
|
5.CollectionUtils
1 2 3 4 5 6 7
|
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.4</version> </dependency>
|
(1)isEmpty()
1 2 3 4
| ArrayList<Object> list = new ArrayList<>(); System.err.println(CollectionUtils.isEmpty(list)); System.err.println(CollectionUtils.isNotEmpty(list));
|
(2)intersection() && union() && subtract()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
ArrayList<Object> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); ArrayList<Object> list1 = new ArrayList<>(); list1.add(3); list1.add(4); list1.add(5); System.err.println("交集:" + CollectionUtils.intersection(list, list1)); System.err.println("并集:" + CollectionUtils.union(list, list1)); System.err.println("如果list在前 差集是:" + CollectionUtils.subtract(list, list1)); System.err.println("如果list1在前 差集是:" + CollectionUtils.subtract(list, list1));
|
6.MapUtils
(1)getInteger()
1 2 3 4
| Map<String, Object> map = new HashMap<>(); map.put("a", "1111"); System.out.println(MapUtils.getInteger(map, "a"));
|
7.FileUtils
1 2 3 4 5 6 7
|
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.11.0</version> </dependency>
|
(1)readFileToString()
1 2 3 4 5 6 7 8 9
| String path = "/Users/geyabo/Documents/study/demo/javaUtilsDemo/src/main/resources/file.txt"; String fileData = null; try { fileData = FileUtils.readFileToString(new File(path), StandardCharsets.UTF_8); } catch (IOException e) { e.printStackTrace(); } System.err.println(fileData);
|
(2)readLines()
1 2 3 4 5 6 7 8 9
| String path = "/Users/geyabo/Documents/study/demo/javaUtilsDemo/src/main/resources/file.txt"; List<String> fileData = null; try { fileData = FileUtils.readLines(new File(path), StandardCharsets.UTF_8); } catch (IOException e) { e.printStackTrace(); } System.err.println(fileData.toString());
|
8.FilenameUtils
(1)getExtension()
1 2 3
| String path = "/Users/geyabo/Documents/study/demo/javaUtilsDemo/src/main/resources/file.txt"; System.err.println(FilenameUtils.getExtension(path));
|
(2)getBaseName()
1 2 3
| String path = "/Users/geyabo/Documents/study/demo/javaUtilsDemo/src/main/resources/file.txt"; System.err.println(FilenameUtils.getBaseName(path));
|
9.Guava
1 2 3 4 5 6
| <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>29.0-jre</version> </dependency>
|
(1)Joiner.on()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| ArrayList<String> list = new ArrayList<>(); list.add("1"); list.add("2"); list.add("3"); list.add(null);
Joiner joiner = Joiner.on("-").skipNulls();
Joiner joiner = Joiner.on(",").useForNull("4"); System.out.println(joiner.join(list));
list.stream().filter(StringUtils::isNotBlank) .collect(Collectors.joining(","))
|
(2)Splitter.on()
1 2 3 4 5 6 7 8 9 10 11
| String str = "1, 2, 3,, 4, ";
Splitter splitter = Splitter.on(",") .omitEmptyStrings() .trimResults() ; Iterable<String> iterable = splitter.split(str); System.err.println(iterable);
|
- omitEmptyStrings():过滤掉空白字符串(不包括””)
- trimResults():去除每个元素的前后空格
(3)CaseFormat
1 2 3 4 5 6 7 8 9 10 11 12
| String str = "stu_name";
System.err.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, str));
System.err.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, str));
String str2 = "stuName"; System.err.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, str2));
System.err.println(CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, str2));
|
(4)Lists
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| ArrayList<String> list = new ArrayList<>(); list.add("a"); list.add("b"); list.add("c"); System.err.println(list);
ArrayList<String> list1 = Lists.newArrayList("a", "b", "c"); System.err.println(list1);
List<Integer> list2 = Ints.asList(1, 2, 3); System.err.println(list2);
List<List<String>> partition = Lists.partition(list1, 2); for (List<String> strings : partition) { System.err.println(partition); }
|
(5)Preconditions
1 2 3 4 5 6 7 8 9 10
|
String str = null; if (str == null) { throw new RuntimeException("参数格式有误!"); }
Preconditions.checkNotNull(str, "参数格式有误!");
Preconditions.checkArgument(str != null, "参数格式有误!");
|
10.Spring工具类
1 2 3 4 5 6
| <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.3.18</version> </dependency>
|
(1)ClassUtils
1 2 3
| String className = "cn.gyb.lang.NumberUtilsTest"; System.err.println(ClassUtils.isPresent(className, null));
|
(2)ReflectionUtils
1 2 3 4 5 6 7 8 9
| ReflectionUtils.doWithFields(ZhangSan.class, field -> { System.err.println(field.getName()); });
ReflectionUtils.doWithMethods(ZhangSan.class, method -> { System.err.println(method.getDeclaringClass().getName() + "的" + method.getName()); });
|
(3)BeanUtils
1 2 3 4 5 6 7 8 9
| ZhangSan zhangSan1 = new ZhangSan(); zhangSan1.setId(1); zhangSan1.setName("李白"); zhangSan1.setMyName("张三");
ZhangSan target = new ZhangSan(); BeanUtils.copyProperties(zhangSan1, target); System.out.println(target);
|
(4)FileCopyUtils
1 2 3 4 5 6 7 8
|
ClassPathResource classPathResource = new ClassPathResource("text/file1.txt");
EncodedResource encodedResource = new EncodedResource(classPathResource, StandardCharsets.UTF_8);
String targetPath = "/Users/geyabo/Documents/study/demo/javaUtilsDemo/src/main/resources/text/file2.txt"; FileCopyUtils.copy(encodedResource.getInputStream(), new FileOutputStream(targetPath));
|