`

常用Java工具类

    博客分类:
  • Java
阅读更多

Ignacio Coloma 说道:作为一个软件开发者,缺乏想象力是最严重的罪过之一。我们经常把事情重复做一遍又一遍,但是我们很少改变这种方式,至少我是这样。经过这些年开发,在我的工具箱里面有了一些每个项目我都需要用到的工具,烦人的重复工作不再是我的事。 

下面这些工具也许你已经用到,让我来仔细介绍它们: 

Lack of imagination is one of our worst sins as software developers. We do the same things over and over again, but we rarely modify our ways: me at least. After some years, these are the tools that made it into my tricks box for everyday tasks. Tiresome operations are not my thing.

Chances are you are already using at least some of these, but here we go anyways:

StringUtils  

这是象面包和奶油一样必须的通用语言库,这个实用工具类包括一些很早以前在String中未包含的重要方法。

StringUtils.isEmpty(null) && StringUtils.isEmpty(""); // true
StringUtils.isBlank("   \n\t");                       // true
StringUtils.substringAfterLast("foo.bar.baz", ".");   // "baz"
StringUtils.substringBeforeLast("foo.bar.baz", ".");  // "foo.bar"
StringUtils.split("foo.bar.baz", '.');                // { "foo", "bar", "baz" }
StringUtils.split("foo,  bar,baz", ", ");             // { "foo", "bar", "baz" }
StringUtils.leftPad("1", 3, '0');                     // "001"
 

 IOUtils and FileUtils  

在一种当你需要手动操作多个文件罕见情况下必须具备的工具,这两个工具很相似(FileUtils操作文件,IOUtils操作InputStream和Reader classes),和捆绑常用IO. 

A must-have for the rare occasions where you need to manipulate files by hand. Both are pretty much alike (FileUtils for File, IOUtils for InputStream and Reader classes) and come bundled in commons-io.

File file1;
File file2;
InputStream inputStream;
OutputStream outputStream;

// copy one file into another
FileUtils.copyFile(file1, file2);
IOUtils.copy(inputStream, outputStream);

// read a file into a String
String s1 = FileUtils.readFileToString(file1);
String s2 = IOUtils.toString(inputStream);

// read a file into a list of Strings, one item per line
List<String> l1 = FileUtils.readLines(file1);
List<String> l2 = IOUtils.readLines(inputStream);

// put this in your finally() clause after manipulating streams
IOUtils.closeQuietly(inputStream);

// return the list of xml and text files in the specified folder and any subfolders
Collection<File> c1 = FileUtils.listFiles(file1, { "xml", "txt" }, true);

// copy one folder and its contents into another
FileUtils.copyDirectoryToDirectory(file1, file2);

// delete one folder and its contents
FileUtils.deleteDirectory(file1);
 

 

 Google collections  
这是我所知道的最好的扩展实现包,其中一些被社区叫嚣着要加入JDK: 

This is the best implementation of a collections extension that I know of. Some of these are shouting to be included in the JDK:

// create an ArrayList with three arguments
List<String> list = Lists.newArrayList("foo", "bar", "baz");

// notice that there is no generics or class cast,
// and still this line does not generate a warning.
Set<String> s = Sets.newConcurrentHashSet();

// intersect and union are basic features of a Set, if you ask me
Set<String> s = Sets.intersect(s1, s2);

// Example  of multiple values in a Map
ListMultimap<String, Validator> validators = new ArrayListMultimap<String, Validator>();
validators.put("save", new RequiredValidator());
validators.put("save", new StringValidator());
validators.put("delete", new NumberValidator());

validators.get("save"); // { RequiredValidator, StringValidator }
validators.get("foo");  // empty List (not null)
validators.values();    // { RequiredValidator, StringValidator, NumberValidator }
 

 java.util.concurrent  
不是每个人都需要这么重的java.util.concurrent,但是很好用:

Not everybody needs the heavy lifting of java.util.concurrent, but the concurrent collections are handy:

 

// a map that may be modified (by the same or different thread) while being iterated
Map<String, Something> repository = new ConcurrentHashMap<String, Something>();

// same with lists. This one is only available with Java 6
List<Something> list = new CopyOnWriteArrayList<Something>();
 
分享到:
评论

相关推荐

    工作11年总结的常用java工具类,上百种方法,开发中绝对用得到

    工作11年总结的常用java工具类,上百种方法,开发中绝对用得到&&工作11年总结的常用java工具类,上百种方法,开发中绝对用得到

    30个常用java工具类

    平时搜藏的常用的工具类,如有侵权,请联系我删除

    java常用工具类

    java常用工具类包含:UUID类,邮件帮助类,字符串帮助类等等

    常用的java工具类

    1.[工具类] 读取、打印输出、保存xml .java 2.[工具类] Java中计算任意两个日期之间的工作天数 .java 3.[工具类] MD5 .java 4.[工具类] 时间工具TimeUtil.java 5.[工具类] 通信服务端...等等20几个常用工具类.

    java常用的工具类整理28个

    java常用的工具类整理28个java常用的工具类整理28个java常用的工具类整理28个

    Java常用工具类UtilClass

    Java常用工具类UtilClass,包括StringUtil、FileUtil、ConnectDB、DateUtil、TimeUtil一些可能用到的工具,实现某些特定的功能,,

    一些java常用的工具类

    平时整理的一些工具类,虽然现在很多框架都有自己的工具类,但是这些都是平时用的比较多的

    java 常用工具类

    java utils 工具类

    Java常用工具类大全

    提供了很丰富的java工具类,包括字符串、数字、日期、文件、图像、编码解码、校验工具、文档操作等。 主要分为以下几种: - 1.通用操作类,例如String、数字、日期、各种校验等 - 2.文档操作,excel、pdf等 - 3.加密...

    精心整理的java常用的26个工具类

    精心整理的26个java常用工具类,如:FastJsonUtil,StringHelper,RandomHelper,FileHelper,HttpClientHelper等等,直接使用maven导入到eclipse中使用即可。

    Java常用工具类大全,工作5年精心整理.zip

    Java常用工具类大全,工作5年精心整理.zip

    java常用的工具类

    主要包含的工具: 1、敏感字的过滤 2、文件的各种操作 3、图片的验证码,图片裁剪 4、获取客户端的ip 5、md5加密,加密解密 6、对象的clone 7、获取操作系统的版本 8、String的操作 9、Date的操作 10、...

    java工具类集合

    常用的Java类集合。

    java常用工具类汇总

    java常用工具类汇总 一个java工具类,包含 关于 字符串 文件 数组 数学 的 常用工具类。 有 相关的 API文档。 内容包括: 1.工具类的API说明文档 2.编译好的Jar文件

    Java常用工具类大全,工作5年精心整理(最新版)

    提供了很丰富的java工具类,包括字符串、数字、日期、文件、图像、编码解码、校验工具、文档操作等。 主要分为以下几种: - 1.通用操作类,例如String、数字、日期、各种校验等 - 2.文档操作,excel、pdf等 - 3.加密...

Global site tag (gtag.js) - Google Analytics