StringUtils、IOUtils and FileUtils

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和Readerclasses),和捆绑常用IO.

Java代码
  1. File file1;  
  2. File file2;  
  3. InputStream inputStream;  
  4. OutputStream outputStream;  
  5.   
  6. // copy one fileinto another  
  7. FileUtils.copyFile(file1, file2);  
  8. IOUtils.copy(inputStream, outputStream);  
  9.   
  10. // read a file intoa String  
  11. String s1 = FileUtils.readFileToString(file1);  
  12. String s2 = IOUtils.toString(inputStream);  
  13.   
  14. // read a file intoa 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 yourfinally() clause after manipulating streams  
  19. IOUtils.closeQuietly(inputStream);  
  20.   
  21. // return the listof xml and text files in the specified folder and anysubfolders  
  22. Collection<File> c1 =FileUtils.listFiles(file1, { "xml","txt"}, true);  
  23.   
  24. // copy one folderand its contents into another  
  25. FileUtils.copyDirectoryToDirectory(file1, file2);  
  26.   
  27. // delete onefolder and its contents  
  28. FileUtils.deleteDirectory(file1); 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值