---
title: "Java工具名单 "
author: "feidaodalian"
date: "2009-08-24T18:34:13Z"
tags: []
slug: "java"
draft: false
lastmod: "2009-08-24T18:34:13Z"
id: 456703
url: "https://feidaodalian.iteye.com/blog/456703"
---

StringUtils 


```java

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. 


> 
> 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： 


> 
> // 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，但是很好用: 


> // 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>();