Apache Commons工具使用
来源:互联网 发布:后藤基次 知乎 编辑:程序博客网 时间:2024/05/12 13:21
1. 简介
Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动。
2. Commons-Lang
1) 重写Object中的方法
一个合格的类应该重写toString,hashCode,equals,compareTo方法,我们来看一下apache如何带我们简化这些操作,通过逐个参数添加从而精细控制那些参数参与比较和输出
public class User implements Comparable<User> { private int id; private String userName; public User(int id, String userName) { this.id = id; this.userName = userName; } //输出格式为: com.blue.sky.commons.User@2626b418[102,yuanda] @Override public String toString() { return new ToStringBuilder(this).append(this.getId()) .append(this.getUserName()).toString(); } @Override public int hashCode() { return new HashCodeBuilder().append(this.getId()) .append(this.getUserName()).hashCode(); } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj.getClass() == User.class) { User test = (User) obj; return new EqualsBuilder().append(this.getId(), test.getId()) .append(this.getUserName(), test.getUserName()).isEquals(); } return false; } @Override public int compareTo(User o) { return new CompareToBuilder().append(this.getId(), o.getId()) .append(this.getUserName(), o.getUserName()).toComparison(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public static void main(String[] args) { User u1 = new User(1, "yd"); User u2 = new User(2, "yj"); System.out.println(u1); System.out.println(u1.hashCode()); System.out.println(u1.equals(u2)); System.out.println(u1.compareTo(u2)); }}
2)ArrayUtils类
简化数组的操作
//测试ArrayUtils操作public static void testArrayUtils() { int[] nums1 = {1, 2, 3, 4, 6, 9}; int[] nums2 = {2, 6, 5}; int[] nums3 = {1, 2, 3, 4, 6, 9}; //两个数组是否相等 System.out.println(Arrays.equals(nums1, nums1)); //true System.out.println(Arrays.equals(nums2, nums1)); //false System.out.println(Arrays.equals(nums1, nums3)); //true int[] nums4 = ArrayUtils.subarray(nums1, 2, 4); System.out.println(ArrayUtils.toString(nums4, "数组为空")); //显示数组的中的元素 ArrayUtils.reverse(nums2); //反转数组 System.out.println(ArrayUtils.toString(nums2, "数组为空")); System.out.println(ArrayUtils.contains(nums2, 5)); //数组中是否包含某个元素 int[] nums5 = ArrayUtils.addAll(nums1, nums2); //合并两个数组 System.out.println(ArrayUtils.toString(nums5));}
3)StringUtils类
用来操作字符串
public static void testStringUtils() { String str = "Hello World"; // isEmpty和isBlank的区别在于isEmpty不会忽略空格, // " "对于这样的字符串isEmpty会认为不是空, // 而isBlank会认为是空,isBlank更常用 System.out.println(StringUtils.isEmpty(str)); System.out.println(StringUtils.isBlank(str)); // strip --> 去除两端的aa // stripStart --> 去除开始位置的hell // stripEnd --> 去除结尾位置的orld System.out.println(StringUtils.strip(str, "aa")); System.out.println(StringUtils.stripStart(str, "Hell")); System.out.println(StringUtils.stripEnd(str, "orld")); System.out.println(str); // 返回字符串在第三次出现A的位置 System.out.println(StringUtils.ordinalIndexOf(str, "A", 3)); // 统计第二个参数在字符串中出现的次数 System.out.println(StringUtils.countMatches(str, "l")); // 判断字符串是否全小写或大写 System.out.println(StringUtils.isAllLowerCase(str)); System.out.println(StringUtils.isAllUpperCase(str)); // isAlpha --> 全部由字母组成返回true // isNumeric -->全部由数字组成返回true // isAlphanumeric -->全部由字母或数字组成返回true // isAlphaSpace -->全部由字母或空格组成返回true // isWhitespace -->全部由空格组成返回true System.out.println(StringUtils.isAlpha("sdf")); System.out.println(StringUtils.isNumeric("12.2")); //不能包含. System.out.println(StringUtils.isAlphanumeric("sd23fd")); System.out.println(StringUtils.isAlphaSpace("fs ss22")); System.out.println(StringUtils.isWhitespace(" f")); // 首字母大写或小写 System.out.println(StringUtils.capitalize(str)); System.out.println(StringUtils.uncapitalize(str)); // 将字符串数组转变为一个字符串,通过","拼接,支持传入iterator和collection System.out.println(StringUtils.join(new String[]{"Hello", "World"}, ","));}
4)FileUtils类
在Java当中,由于IO架构采用了装饰器模式,以至于JavaIO操作可谓是繁杂无比,特别经常需要访问文件系统,操作文件夹/文件,读取字符流转换成字符串等操作,都是一些重复性的操作
public class TestFile { public static final String TEST_DIRECTORY_PATH_1 = "D:/directory1"; public static final String TEST_DIRECTORY_PATH_2 = "D:/directory2"; //目录 public static final File DIRECTORY_1 = new File(TEST_DIRECTORY_PATH_1); public static final File DIRECTORY_2 = new File(TEST_DIRECTORY_PATH_2); public static final String TEST_FILE_PATH_1 = "D:/directory1/1.txt"; public static final String TEST_FILE_PATH_2 = "D:/directory2/3.txt"; //文件 public static final File FILE_1 = new File(TEST_FILE_PATH_1); public static final File FILE_2 = new File(TEST_FILE_PATH_2); public static final String FILE_NAME = "D:/directory1/helloworld.txt"; //对目录和文件的操作 public static void testFileUtils() throws IOException { // 清空某目录下的所有目录,含文件夹和文件 FileUtils.cleanDirectory(DIRECTORY_2); // 将参数1目录下的全部内容复制到参数2目录 FileUtils.copyDirectory(DIRECTORY_1, DIRECTORY_2); // 将参数1目录整个复制到参数2目录下 FileUtils.copyDirectoryToDirectory(DIRECTORY_1, DIRECTORY_2); // copy參數1文件到參數2 FileUtils.copyFile(FILE_1, FILE_2); // copy參數1文件到參數2目錄下 FileUtils.copyFileToDirectory(FILE_1, DIRECTORY_2); // 将文件转为InputStrem FileUtils.openInputStream(FILE_1); // FileUtils.openOutputStream(FILE_1); // 读取文件转为字节数组 //FileUtils.readFileToByteArray(FILE_1); // 读取文件转换为String类型,方便文本读取 System.out.println(FileUtils.readFileToString(FILE_1, "UTF-8")); // 写字符串到参数1文件中 FileUtils.writeStringToFile(FILE_1, "test", "UTF-8"); System.out.println(FileUtils.readFileToString(FILE_1, "UTF-8")); // 强制删除文件 //FileUtils.forceDelete(FILE_1); } //对文件名的操作 public static void testFileUtils2() { // 获取文件后缀名 String extensionName = FilenameUtils.getExtension(FILE_NAME); System.out.println(extensionName); //txt // 获取文件路径,忽略分割符 / String fullPath1 = FilenameUtils.getFullPathNoEndSeparator(FILE_NAME); System.out.println(fullPath1); //D:/directory1 // 获取文件路径,不忽略分隔符 String fullPath2 = FilenameUtils.getFullPath(FILE_NAME); System.out.println(fullPath2); //D:/directory1/ // 获取文件名,不包含后缀 String baseName = FilenameUtils.getBaseName(FILE_NAME); System.out.println(baseName); //helloworld // 获取文件名,含后缀 String name = FilenameUtils.getName(FILE_NAME); System.out.println(name); //helloworld.txt // 获取盘符 String prefix = FilenameUtils.getPrefix(FILE_NAME); System.out.println(prefix); //D:/ // 通配符匹配 Boolean isMatch = FilenameUtils.wildcardMatch(FILE_NAME, "D:/directory1*"); System.out.println(isMatch); //true } public static void main(String[] args) throws IOException { testFileUtils(); testFileUtils2(); }}
其他的等具体使用到了再添加….
0 0
- Apache Commons工具使用
- 使用 Apache Commons CLI 开发命令行工具
- 使用 Apache Commons CLI 开发命令行工具
- Apache Commons工具集简介-使用
- 使用 Apache Commons CLI 开发命令行工具
- 使用Apache Commons CLI开发命令行工具
- Apache Commons 工具集使用简介
- Apache Commons 工具集使用简介
- 使用 Apache Commons CLI 开发命令行工具
- 使用Apache Commons CLI 开发命令行工具
- Apache commons-io 工具类的使用
- Apache Commons 工具集使用简介
- 使用Apache Commons CLI开发命令行工具
- Apache Commons工具集
- Apache Commons工具集
- Apache Commons工具
- 使用Apache.commons.io文件工具类FileUtils,IOUtils使用
- Apache通用日志工具commons-logging和Log4j使用总结
- Universal-Image-Loader源码阅读(21)-memory总结
- 详解Echarts 绘制柱状图
- Android 开发最佳实践
- 预处理
- [Java]知乎下巴第3集:来人啊快把知乎的答案装到篮子里去
- Apache Commons工具使用
- C++ 构造/析构函数中的异常处理
- POJ 3160 Father Christmas flymouse 强连通+Spfa
- java代码实现加密解密(MD5签名(同步接口))
- Eclipse远程调试WEB程序
- [Java]知乎下巴第4集:再把抓到篮子里的知乎塞到硬盘里吧
- 见过的最全的iOS面试题
- Unique Paths
- STM32中断