服务器之家

服务器之家 > 正文

Java常用工具类汇总 附示例代码

时间:2021-09-03 14:05     来源/作者:GooReey

一、FileUtils

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
private static void fileUtilsTest() {
    try {
        //读取文件内容
        String readFileToString = FileUtils.readFileToString(new File("D:\\guor\\data\\test20211022000000.txt"));
        System.out.println(readFileToString);
        //删除文件夹
        FileUtils.deleteDirectory(new File("D:\\guor1\\data1"));
        FileUtils.deleteQuietly(new File("D:\\guor\\data"));
        //清空文件夹
        FileUtils.cleanDirectory(new File("D:\\guor\\data"));
        //判断文件内容是否一致
        boolean contentEquals = FileUtils.contentEquals(new File("D:\\guor\\data\\test20211022000000.txt"), new File("D:\\guor\\data\\testNew20211021000000.txt"));
        boolean contentEquals2 = FileUtils.contentEquals(new File("D:\\guor\\data\\中文20211021100000.txt"), new File("D:\\guor\\data\\中文New20211022100000.txt"));
        System.out.println(contentEquals+","+contentEquals2);
        //拷贝特定类型的文件
        FileUtils.copyDirectory(new File("D:\\guor\\data"), new File("D:\\guor\\data2"), new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                if(pathname.getName().startsWith("test")) {
                    return true;
                }
                return false;
            }
        });
        FileUtils.moveDirectory(new File("D:\\guor\\data"), new File("D:\\guor\\data3"));
        FileUtils.moveFileToDirectory(new File("D:\\guor\\data"), new File("D:\\guor\\data3"), true);
        FileUtils.moveToDirectory(new File("D:\\guor\\data"), new File("D:\\guor\\data4"), true);
        boolean directoryContains = FileUtils.directoryContains(new File("D:\\guor\\data"), new File("D:\\guor\\data\\test20211022000000.txt"));
        System.out.println(directoryContains);
        directoryContains = FileUtils.directoryContains(new File("D:\\guor\\data"), new File("*.txt"));
        System.out.println(directoryContains);
        //获取某文件夹下特定格式文件
        File[] listFiles = new File("D:\\guor\\data").listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.startsWith("test");
            }
        });
 
        System.out.println(Arrays.toString(listFiles));
        //获取系统temp文件夹路径
        File tempDirectory = FileUtils.getTempDirectory();
        System.out.println(tempDirectory);
        //获取系统用户文件夹路径
        File userDirectory = FileUtils.getUserDirectory();
        System.out.println(userDirectory);
 
        //查看是否是新建的文件夹
        SimpleDateFormat sdf =new SimpleDateFormat("yyyyMMddHHmmss" );
        Date parse = sdf.parse("20210206134900");
        boolean fileNewer = FileUtils.isFileNewer(new File("D:\\guor\\data"), parse);
        System.out.println(fileNewer);
        //更新文件修改时间,如果不存在,则新建;
        FileUtils.touch(new File("D:\\guor\\data\\test20211022000000.txt"));
        //延迟查看文件是否存在
        boolean waitFor = FileUtils.waitFor(new File("D:\\guor\\data\\testNew20211021000000.txt"), 5);
        System.out.println(waitFor);
    } catch (Exception e) {
        System.out.println(e);
    }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//根据文件修改时间排序
public static void test02() {
    String dir = "D:\\data";
    File[] listFiles = new File(dir).listFiles();
    List<File> asList = Arrays.asList(listFiles);
    for(File file:listFiles) {
        System.out.println(file);
    }
    Collections.sort(asList, new Comparator<File>() {
        @Override
        public int compare(File o1, File o2) {
            if(o1.lastModified()<o2.lastModified()) {
                return -1;
            }else if(o1.lastModified()>o2.lastModified()) {
                return 1;
            }else {
                return 0;
            }
        }
    });
    System.out.println("sort...");
    for(File file:listFiles) {
        System.out.println(file+","+file.lastModified());
    }
}

二、StringUtil

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
private static void stringUtilsTest() {
    String str = "hello world";
    //将第一个字符大写
    String capitalize = StringUtils.capitalize(str);
    System.out.println(capitalize);//Hello
    //两边空格补齐
    String center = StringUtils.center(str, 7);
    System.out.println(center);// hello
    String center = StringUtils.center(str, 7, "*");
    System.out.println(center);//*hello*
    //chomp剔除字符串最后一个是\n或\r\或\r\n
    String chomp = StringUtils.chomp(str);
    System.out.println(chomp);
    //chop剔除字符串最后一个字符(如果最后一个是\n或\r\或\r\n也剔除掉)
    String chop = StringUtils.chop(str);
    System.out.println(chop);//hello worl
    //是否包含任意字符
    boolean containsAny = StringUtils.containsAny(str, "W");
    System.out.println(containsAny);
    //是否包含任意字符,忽略大小写
    containsAny = StringUtils.containsIgnoreCase(str, "W");
    System.out.println(containsAny);
 
    //有意思
    int indexOfAnyBut = StringUtils.indexOfAnyBut(str, "hello orld");
    System.out.println(indexOfAnyBut);
 
    //两个字符串从第几位开始不同
    int indexOfDifference = StringUtils.indexOfDifference(str, str);
    System.out.println(indexOfDifference);
    indexOfDifference = StringUtils.indexOfDifference(str, str+"zs");
    System.out.println(indexOfDifference);
    indexOfDifference = StringUtils.indexOfDifference(str, "he llo");
    System.out.println(indexOfDifference);
 
    //判断是否是正整数
    boolean isNumeric = StringUtils.isNumeric(str);
    System.out.println(isNumeric);//false
    isNumeric = StringUtils.isNumeric("1234dd");
    System.out.println("1234dd:"+isNumeric);//false
    isNumeric = StringUtils.isNumeric("1234 ");
    System.out.println("1234 :"+isNumeric);//false
    isNumeric = StringUtils.isNumeric("1.234");
    System.out.println("1.234:"+isNumeric);//false
    isNumeric = StringUtils.isNumeric("1234");
    System.out.println("1234:" + isNumeric);//true
    isNumeric = StringUtils.isNumeric("-1234");
    System.out.println("-1234:" + isNumeric);//false
    //判断是否是正整数或空格
    boolean isNumeric = StringUtils.isNumericSpace(str);
 
    //将数组变为字符串,并添加字符进行分割
    Object[] array = new Object[] {"zs",18,"男",666,777};
    String join = StringUtils.join(array);
    System.out.println(Arrays.toString(array));
    join = StringUtils.join(array, "*");
    System.out.println(join);
    Object[] array = new Object[] {"zs",18,"男",666,777};
    String join = StringUtils.join(array, ";", 2, 5);
    System.out.println(join);
 
    String remove = StringUtils.remove(str, "hello");
    System.out.println(remove);// world
    String repeat = StringUtils.repeat(str, 3);
    System.out.println(repeat);//hello worldhello worldhello world
    repeat = StringUtils.repeat(str, ",", 3);
    System.out.println(repeat);//hello world,hello world,hello world
 
    //分别替换
    String[] list =  new String[] {"hello", "girl"};
    String[] replacementList =  new String[] {"say", "woman"};
    String str2 = "hello world,hello boy,hello girl.";
    String replaceEach = StringUtils.replaceEach(str2, list, replacementList);
    System.out.println(replaceEach);//say world,say boy,say woman.
 
    //只替换第一个匹配的字符串
    String replaceOnce = StringUtils.replaceOnce("hello world,hello boy,hello girl.", "hello", "say");
    System.out.println(replaceOnce);//say world,hello boy,hello girl.
 
    //截取某字符后面的部分
    String substringAfterLast = StringUtils.substringAfterLast("hello world,hello boy,hello girl.", " ");
    System.out.println(substringAfterLast);//girl.
 
    String substringBeforeLast = StringUtils.substringBeforeLast("hello world,hello boy,hello girl.", " ");
    System.out.println(substringBeforeLast);//hello world,hello boy,hello
}

三、CollectionUtils

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
private static void collectionUtilsTest() {
    List<String> list = new ArrayList<String>();
    list.add("zhangsan");
    list.add("dalian");
    list.add("gateway");
    list.add("gateway");
    list.add("28");
    list.add("35");
    //查找第一个符合条件的元素
    Object find = CollectionUtils.find(list, new Predicate() {
        @Override
        public boolean evaluate(Object arg0) {
            if(StringUtils.isNumeric(arg0.toString())) {
                return false;
            }
            return true;
        }
    });
 
    //排除不符合条件的元素
    Collection select = CollectionUtils.select(list, new Predicate() {
        @Override
        public boolean evaluate(Object arg0) {
            if(StringUtils.isNumeric(arg0.toString())) {
                return false;
            }
            return true;
        }
    });
    System.out.println(list);//[zhangsan, dalian, 28]
    System.out.println(find);//zhangsan
    System.out.println(select);//[zhangsan, dalian]
 
 
    List<String> list2 = new ArrayList<String>();
    list2.add("zhangsan");
    list2.add("dalian2");
    list2.add("282");
    //查看两个集合中是否有相同的元素
    boolean containsAny = CollectionUtils.containsAny(list, list2);
    System.out.println(containsAny);//true
 
    //查找集合中某元素的个数
    int countMatches = CollectionUtils.countMatches(list, new Predicate() {
        @Override
        public boolean evaluate(Object arg0) {
            if(StringUtils.isNumeric(arg0.toString())) {
                return true;
            }
            return false;
        }
    });
    System.out.println(countMatches);
 
    //查找集合中是否存在某元素
    boolean exists = CollectionUtils.exists(list, new Predicate() {
        @Override
        public boolean evaluate(Object arg0) {
            if(StringUtils.isNumeric(arg0.toString())) {
                return true;
            }
            return false;
        }
    });
 
    System.out.println(exists);
 
    //删除集合中的某些元素
    System.out.println(list);//[zhangsan, dalian, 打法, 28, 35]
    CollectionUtils.filter(list, new Predicate() {
        @Override
        public boolean evaluate(Object arg0) {
            if(StringUtils.isNumeric(arg0.toString())) {
                return false;
            }
            return true;
        }
    });
 
    System.out.println(list);//[zhangsan, dalian, 打法]
 
    //查找集合中符合条件的第一个元素
    Object find = CollectionUtils.find(list, new Predicate() {
        @Override
        public boolean evaluate(Object arg0) {
            if(StringUtils.isNumeric(arg0.toString())) {
                return false;
            }
            return true;
        }
    });
    System.out.println(find);//zhangsan
 
    //闭包回调函数
    System.out.println(list);
    CollectionUtils.forAllDo(list, new Closure() {
        @Override
        public void execute(Object arg0) {
            if(StringUtils.isNumeric(arg0.toString())) {
                work(arg0);
            }
        }
    });
    System.out.println(list);
 
    //查找集合中元素的个数
    System.out.println(list);//[zhangsan, dalian, gateway, gateway, 28, 35]
    Map cardinalityMap = CollectionUtils.getCardinalityMap(list);
    System.out.println(cardinalityMap);//{35=1, 28=1, zhangsan=1, dalian=1, gateway=2}
 
    //查找两个集合中的相同元素
    List<String> list2 = new ArrayList<String>();
    list2.add("lisi");
    list2.add("beijing");
    list2.add("gateway");
    list.add("19");
    Collection intersection = CollectionUtils.intersection(list, list2);
    System.out.println(intersection);
 
    //获取两个List<Map<String, Object>>中相同的部分,可以用于对比两个库中的相同表数据是否相等
    List<Map<String, Object>> list3 = new ArrayList<Map<String,Object>>();
    List<Map<String, Object>> list4 = new ArrayList<Map<String,Object>>();
    Map<String, Object> map1 = new HashMap<String, Object>();
    map1.put("name", "zhangsan");
    map1.put("age", 18);
    map1.put("address", "dalian");
    Map<String, Object> map2 = new HashMap<String, Object>();
    map2.put("name", "张三");
    map2.put("age", "十八");
    map2.put("address", "大连");
    list3.add(map1);
    list3.add(map2);
 
    Map<String, Object> map3 = new HashMap<String, Object>();
    map3.put("name", "李四");
    map3.put("age", "十八");
    map3.put("address", "大连");
    list4.add(map2);
    list4.add(map1);
    list3.add(map3);
    System.out.println(list3);
    System.out.println(list4);
    Collection intersection = CollectionUtils.intersection(list3, list4);
    System.out.println(intersection);//[{address=dalian, name=zhangsan, age=18}]
 
    //判断两个集合是否相同
    boolean equalCollection = CollectionUtils.isEqualCollection(list3, list4);
    System.out.println(equalCollection);
 
    判断param2是否包含param1,且不能完全相同
    System.out.println(list3);//[{address=dalian, name=zhangsan, age=18}, {address=大连, name=张三, age=十八}, {address=大连, name=李四, age=十八}]
    System.out.println(list4);//[{address=大连, name=张三, age=十八}, {address=dalian, name=zhangsan, age=18}]
    boolean properSubCollection = CollectionUtils.isProperSubCollection(list4, list3);
    System.out.println(properSubCollection);//true
 
    //断定某集合是否含有某元素,如果包含则抛异常(我觉得应该少用为妙)
    System.out.println(list);//[zhangsan, dalian, gateway, gateway, 28, 35]
    Collection predicatedCollection = CollectionUtils.predicatedCollection(list, new Predicate() {
        @Override
        public boolean evaluate(Object object) {
            if(StringUtils.isNumeric(object.toString())) {
                return false;
            }
            return true;
        }
    });
    System.out.println(predicatedCollection);//Exception in thread "main" java.lang.IllegalArgumentException: Cannot add Object '28' - Predicate rejected it
    List<String> list2 = new ArrayList<String>();
    list2.add("gateway");
    System.out.println(list);
    System.out.println(list2);
    //删除list1中的list2
    Collection removeAll = CollectionUtils.removeAll(list, list2);
    System.out.println(removeAll);
 
    //反转数组
    String[] array = new String[] {"zs","ls","ww"};
    System.out.println(StringUtils.join(array, ","));//zs,ls,ww
    CollectionUtils.reverseArray(array);
    System.out.println(StringUtils.join(array, ","));//ww,ls,zs
 
    删除list1中的list2
    List<String> list2 = new ArrayList<String>();
    list2.add("zhangsan");
    list2.add("dalian");
    Collection subtract = CollectionUtils.subtract(list, list2);
    System.out.println(subtract);
 
    //更改集合中的特定值
    Collection collect = CollectionUtils.collect(list, new Transformer() {
        @Override
        public Object transform(Object arg0) {
            if(StringUtils.isNumeric(arg0.toString())) {
                return 15;
            }
            return arg0;
        }
    });
    System.out.println(list);//[zhangsan, dalian, 28, 35]
    System.out.println(collect);//[zhangsan, dalian, 15, 15]
 
    //更改集合中的特定值,改变集合本身
    System.out.println(list);//[zhangsan, dalian, gateway, gateway, 28, 35]
    CollectionUtils.transform(list, new Transformer() {
 
        @Override
        public Object transform(Object input) {
            if(StringUtils.isNumeric(input.toString())) {
                return 15;
            }
            return input;
        }
    });
    System.out.println(list);//[zhangsan, dalian, gateway, gateway, 15, 15]
    List<String> list2 = new ArrayList<String>();
    list2.add("docker");
    list2.add("zhangsan");
    System.out.println(list);
    System.out.println(list2);
    //将list和list2中的元素合并,但去除list2中的重复元素
    Collection union = CollectionUtils.union(list, list2);
    System.out.println(union);
}
?
1
2
3
private static void work(Object obj){
    System.out.println("闭包回调函数:我是数字,"+obj);
}

四、ArrayUtils

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private static void arrayUtilsTest() {
    String[] array =new String[] {"zs", "ls"};
 
    //增加元素
    Object[] add = ArrayUtils.add(array, "ww");
    System.out.println(StringUtils.join(add, ","));//zs,ls,ww
 
    //合并数组
    String[] array2 =new String[] {"ww", "ss"};
    Object[] addAll = ArrayUtils.addAll(array1, array2);
    System.out.println(StringUtils.join(addAll, ","));//zs,ls,ww,ss
 
    //克隆数组
    Object[] clone = ArrayUtils.clone(array);
    System.out.println(StringUtils.join(clone, ","));//zs,ls
 
 
    boolean contains = ArrayUtils.contains(array, "ww");
    boolean contains1 = ArrayUtils.contains(array, "zs");
    System.out.println(contains+","+contains1);//false,true
}

到此这篇关于Java常用工具类汇总 附示例代码的文章就介绍到这了,更多相关Java常用工具类内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/guorui_java/article/details/115529750

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
返回顶部