Java工具类

纵然,自己手写工具方法会很爽,但有些工具类还是值得参考:

Display-----

 

StringUtils

The bread and butter of the commons-lang library, this utility class includes some methods that should seriously have been included in String long time ago.

 

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

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.

 

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

 

ETC

 

Google collections

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

 

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

你可能感兴趣的:(java)