Java常用工具类

时间:2021-05-21 22:10:45
 

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

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

StringUtils

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

Java代码
  1. StringUtils.isEmpty( null ) && StringUtils.isEmpty( "" ); // true
  2. StringUtils.isBlank(" \n\t" ); // true
  3. StringUtils.substringAfterLast("foo.bar.baz" , "." ); // "baz"
  4. StringUtils.substringBeforeLast("foo.bar.baz" , "." ); // "foo.bar"
  5. StringUtils.split("foo.bar.baz" , '.' ); // { "foo", "bar", "baz" }
  6. StringUtils.split("foo, bar,baz" , ", " ); // { "foo", "bar", "baz" }
  7. StringUtils.leftPad("1" , 3 , '0' ); // "001"


IOUtils and FileUtils

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

Java代码
  1. File file1;
  2. File file2;
  3. InputStream inputStream;
  4. OutputStream outputStream;
  5. // copy one file into another
  6. FileUtils.copyFile(file1, file2);
  7. IOUtils.copy(inputStream, outputStream);
  8. // read a file into a String
  9. String s1 = FileUtils.readFileToString(file1);
  10. String s2 = IOUtils.toString(inputStream);
  11. // read a file into a list of Strings, one item per line
  12. List<String> l1 = FileUtils.readLines(file1);
  13. List<String> l2 = IOUtils.readLines(inputStream);
  14. // put this in your finally() clause after manipulating streams
  15. IOUtils.closeQuietly(inputStream);
  16. // return the list of xml and text files in the specified folder and any subfolders
  17. Collection<File> c1 = FileUtils.listFiles(file1, { "xml" , "txt" }, true );
  18. // copy one folder and its contents into another
  19. FileUtils.copyDirectoryToDirectory(file1, file2);
  20. // delete one folder and its contents
  21. FileUtils.deleteDirectory(file1);


Google collections

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

Java代码
  1. // create an ArrayList with three arguments
  2. List<String> list = Lists.newArrayList("foo" , "bar" , "baz" );
  3. // notice that there is no generics or class cast,
  4. // and still this line does not generate a warning.
  5. Set<String> s = Sets.newConcurrentHashSet();
  6. // intersect and union are basic features of a Set, if you ask me
  7. Set<String> s = Sets.intersect(s1, s2);
  8. // Example of multiple values in a Map
  9. ListMultimap<String, Validator> validators = new ArrayListMultimap<String, Validator>();
  10. validators.put("save" , new RequiredValidator());
  11. validators.put("save" , new StringValidator());
  12. validators.put("delete" , new NumberValidator());
  13. validators.get("save" ); // { RequiredValidator, StringValidator }
  14. validators.get("foo" ); // empty List (not null)
  15. validators.values(); // { RequiredValidator, StringValidator, NumberValidator }


java.util.concurrent

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

Java代码
  1. // a map that may be modified (by the same or different thread) while being iterated
  2. Map<String, Something> repository = new ConcurrentHashMap<String, Something>();
  3. // same with lists. This one is only available with Java 6
  4. List<Something> list = new CopyOnWriteArrayList<Something>();