`
lighter
  • 浏览: 495871 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

妙用Commons良药<一>

阅读更多
妙用commons良药<一>
这一篇文章主要是讲Commons IO的一些内容.
Commons IO提供了org.apache.commons.io.CopyUtils类来将某个InputStream,Reader,byte[]数据或字符串的内容拷贝到OutputStream或Writer.

    Writer writer = new FileWriter( "test.dat" );
    InputStream inputStream = 
        getClass( ).getResourceAsStream("./test.resource");
    CopyUtils.copy( inputStream, writer );
    writer.close( );
    inputStream.close( );



如果需要把信息从Reader或InputStream拷贝到字符串中,请使用IOUtils.toString()方法.
    InputStream inStream = url.openStream( );
    String contents = IOUtils.toString( inStream );



通过org.apache.commons.io.IOUtils,你可以很好地关闭某个InputStream,OutputStream,Reader或Writer,而不必担心null或IOException.
try {
    File file = new File( "test.dat" );
    reader = new FileReader( file );
    result = CopyUtils.toString( reader );
} catch( IOException ioe ) {
    System.out.println( "Unable to copy file test.dat to a String." );
} finally {
    IOUtils.closeQuietly( reader );
}



使用FileUtils.byteCountToDisplaySize()生成一个字符串,该字符串含有有一个近似的比较好理解的文件的相对大小的值.
   File file = new File("project.xml");
   long bytes = file.length( );
   String display = FileUtils.byteCountToDisplaySize( bytes );



如果需要将一个文件拷贝为另一个文件,或者需要将某个文件拷贝到某一个目录中,可以使用如下的代码:
拷贝为另一个文件:
    File src = new File( "test.dat" );
    file dest = new File( "test.dat.bak" );
    FileUtils.copyFile( src, dest );

拷贝到某一个目录:
    File src = new File( "test.dat" );
    File dir = new File( "./temp" );
    FileUtils.copyFileToDirectory( src, dir );



使用Commons IO,你也可以很方便把字符串的内容写入文件中去,具体的过程不用怎么理会:
  String string = "Blah blah blah";
  File dest = new File( "test.tmp" );
  FileUtils.writeStringToFile( dest, string);



当然,有另外的一个功能,可以将URL的内容存入文件中去:
    URL src = new URL( "http://www.nytimes.com" );
    File dest = new File( "times.html" );
    FileUtils.copyURLToFile( src, dest );



如果你需要删除一个目录下的所有内容(包括其目录),可以这样做:
    File dir = new File( "temp" );
    FileUtils.deleteDirectory( dir );
如果只想清空目录下所有内容,并不删除该目录,可以这样写:FileUtils.cleanDirectory( dir );


很简单地,可以得到一个目录的大小:
File dir = new File( "temp" );
long dirSize = FileUtils.sizeOfDirectory( );



如果你想得到某个目录下所有以.txt结尾的文件,可以如下这样做:
import java.io.FilenameFilter;
import org.apache.commons.io.filefilter.SuffixFileFilter;
import org.apache.commons.lang.ArrayUtils;

File rootDir = new File(".");
FilenameFilter fileFilter = new SuffixFileFilter(".txt");
String[] txtFiles = rootDir.list( fileFilter );
System.out.println( ArrayUtils.toString( txtFiles ) );



举另一个例子,怎样列出目录中以.htm和.html结尾的文件
import org.apache.commons.io.filefilter.AndFileFilter;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.filefilter.NotFileFilter;
import org.apache.commons.io.filefilter.OrFileFilter;
import org.apache.commons.io.filefilter.SuffixFileFilter;
import org.apache.commons.lang.ArrayUtils;

IOFileFilter htmlFilter = 
    new OrFileFilter( new SuffixFileFilter("htm"),new SuffixFileFilter("html") ); //复合两个SuffixFileFilter
IOFileFilter notDirectory = new NotFileFilter( DirectoryFileFilter.INSTANCE );    //拒绝选择目录
FilenameFilter fileFilter = new AndFileFilter( htmlFilter, notDirectory );
String[] htmlFiles = rootDir.list(fileFilter);
System.out.println( ArrayUtils.toString( htmlFiles ));


注:文中代码来之<<Jakarta Commons Cookbook>>一书第十章
文章待续,会写一些关于commons的笔录下来
分享到:
评论
1 楼 feidi 2007-09-13  
用了一下FileUtils.writeStringToFile( dest, string); 速度极慢...不知为何?

相关推荐

    Apache Commons 所有包最新版本 含SRC (6/7)

    commons-attributes-2.2-src.zip&lt;br&gt;commons-attributes-2.2.zip&lt;br&gt;commons-beanutils-1.8.0-BETA-src.zip&lt;br&gt;commons-beanutils-1.8.0-BETA.zip&lt;br&gt;commons-betwixt-0.8-src.zip&lt;br&gt;commons-betwixt-0.8.zip&lt;br&gt;...

    Apache Commons 所有包最新版本 含SRC (5/7)

    commons-attributes-2.2-src.zip&lt;br&gt;commons-attributes-2.2.zip&lt;br&gt;commons-beanutils-1.8.0-BETA-src.zip&lt;br&gt;commons-beanutils-1.8.0-BETA.zip&lt;br&gt;commons-betwixt-0.8-src.zip&lt;br&gt;commons-betwixt-0.8.zip&lt;br&gt;...

    Apache Commons 所有包最新版本 含SRC (1/7)

    commons-attributes-2.2-src.zip&lt;br&gt;commons-attributes-2.2.zip&lt;br&gt;commons-beanutils-1.8.0-BETA-src.zip&lt;br&gt;commons-beanutils-1.8.0-BETA.zip&lt;br&gt;commons-betwixt-0.8-src.zip&lt;br&gt;commons-betwixt-0.8.zip&lt;br&gt;...

    Apache Commons 所有包最新版本 含SRC (3/7)

    commons-attributes-2.2-src.zip&lt;br&gt;commons-attributes-2.2.zip&lt;br&gt;commons-beanutils-1.8.0-BETA-src.zip&lt;br&gt;commons-beanutils-1.8.0-BETA.zip&lt;br&gt;commons-betwixt-0.8-src.zip&lt;br&gt;commons-betwixt-0.8.zip&lt;br&gt;...

    Apache Commons 所有包最新版本 含SRC (7/7)

    commons-attributes-2.2-src.zip&lt;br&gt;commons-attributes-2.2.zip&lt;br&gt;commons-beanutils-1.8.0-BETA-src.zip&lt;br&gt;commons-beanutils-1.8.0-BETA.zip&lt;br&gt;commons-betwixt-0.8-src.zip&lt;br&gt;commons-betwixt-0.8.zip&lt;br&gt;...

    Apache Commons 所有包最新版本 含SRC (4/7)

    commons-attributes-2.2-src.zip&lt;br&gt;commons-attributes-2.2.zip&lt;br&gt;commons-beanutils-1.8.0-BETA-src.zip&lt;br&gt;commons-beanutils-1.8.0-BETA.zip&lt;br&gt;commons-betwixt-0.8-src.zip&lt;br&gt;commons-betwixt-0.8.zip&lt;br&gt;...

    aspose-words-15.8.0-jdk16.jar.rar

    添加依赖: &lt;dependency&gt; &lt;groupId&gt;com.itextpdf&lt;/groupId&gt; &lt;artifactId&gt;itext7-core... &lt;groupId&gt;org.apache.commons&lt;/groupId&gt; &lt;artifactId&gt;commons-io&lt;/artifactId&gt; &lt;version&gt;1.3.2&lt;/version&gt; &lt;/dependency&gt;

    Apache Commons 所有包最新版本 含SRC (2/7)

    commons-attributes-2.2-src.zip&lt;br&gt;commons-attributes-2.2.zip&lt;br&gt;commons-beanutils-1.8.0-BETA-src.zip&lt;br&gt;commons-beanutils-1.8.0-BETA.zip&lt;br&gt;commons-betwixt-0.8-src.zip&lt;br&gt;commons-betwixt-0.8.zip&lt;br&gt;...

    client发送短信验证码(云之讯)

    &lt;artifactId&gt;commons-httpclient&lt;/artifactId&gt; &lt;version&gt;3.0&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;commons-lang&lt;/groupId&gt; &lt;artifactId&gt;commons-lang&lt;/artifactId&gt; &lt;version&gt;2.6&lt;/version&gt; &lt;/...

    基于springboot演示resuful api、mock请求、validate验证、异常捕捉、aop切面编程+源代码+文档说

    &lt;version&gt;${commons-lang3.version}&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.projectlombok&lt;/groupId&gt; &lt;artifactId&gt;lombok&lt;/artifactId&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.spring...

    java实现poi 在线预览,excel,word直接在页面显示,附带文件上传,多文件上传

    &lt;artifactId&gt;commons-collections4&lt;/artifactId&gt; &lt;version&gt;4.1&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.apache.poi&lt;/groupId&gt; &lt;artifactId&gt;poi&lt;/artifactId&gt; &lt;version&gt;3.17&lt;/version&gt; &lt;/...

    struts框架jar包

    struts-1.3.8 包(&lt;br&gt;&lt;br&gt;antlr-2.7.2.jar&lt;br&gt;bsf-2.3.0.jar&lt;br&gt;commons-beanutils-1.7.0.jar&lt;br&gt;commons-chain-1.1.jar&lt;br&gt;commons-digester-1.8.jar&lt;br&gt;commons-fileupload-1.1.1.jar&lt;br&gt;commons-io-1.1.jar&lt;br&gt;...

    JSF与Shale开发用包

    其中包含:shale-core.jar&lt;br&gt;commons-beanutils.jar&lt;br&gt;commons-chain.jar&lt;br&gt;commons-codec.jar&lt;br&gt;commons-collections.jar&lt;br&gt;commons-digester.jar&lt;br&gt;commons-el.jar&lt;br&gt;commons-fileupload.jar&lt;br&gt;commons-...

    Java 课程设计 高校公寓管理系统

    &lt;artifactId&gt;standard&lt;/artifactId&gt; &lt;version&gt;1.1.2&lt;/version&gt; &lt;artifactId&gt;jstl&lt;/artifactId&gt; ... &lt;artifactId&gt;commons-io&lt;/artifactId&gt; &lt;version&gt;2.11.0&lt;/version&gt; &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;

    Java 登录注册案例实例

    &lt;artifactId&gt;commons-io&lt;/artifactId&gt; &lt;version&gt;2.11.0&lt;/version&gt; &lt;artifactId&gt;mybatis&lt;/artifactId&gt; &lt;version&gt;3.5.5&lt;/version&gt; &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt; &lt;version&gt;8.0.28&lt;/version&gt; ...

    springcloud+springcloud alibaba完整学习项目

    &lt;module&gt;cloud-api-commons&lt;/module&gt; &lt;module&gt;cloud-eureka-server7001&lt;/module&gt; &lt;module&gt;cloud-eureka-server7002&lt;/module&gt; &lt;module&gt;cloud-comsumer-order80&lt;/module&gt; &lt;module&gt;cloud-provider-payment8001&lt;/...

    spring-hibernate-dwr实例

    collections-2.1.1.jar&lt;br&gt;commons-logging-1.0.4.jar&lt;br&gt;dom4j-1.6.1.jar&lt;br&gt;ehcache-1.1.jar&lt;br&gt;hibernate3.jar&lt;br&gt;jaas.jar&lt;br&gt;jaxen-1.1-beta-7.jar&lt;br&gt;jdbc2_0-stdext.jar&lt;br&gt;jta.jar&lt;br&gt;log4j-1.2.11.jar&lt;br&gt;...

    spring+struts+hibernate+dwr+jstl做的实例

    api.jar&lt;br&gt;jxl.jar&lt;br&gt;itext-1.3.jar&lt;br&gt;poi-2.5.1.jar&lt;br&gt;cos.jar&lt;br&gt;velocity-1.5.jar&lt;br&gt;velocity-tools-view-1.3.jar&lt;br&gt;commons-codec.jar&lt;br&gt;antlr.jar&lt;br&gt;commons-beanutils.jar&lt;br&gt;commons-digester.jar&lt;br...

    ssm的jar包

    ssm在maven项目管理下的pom文件,jar包依赖 &lt;dependency&gt; &lt;groupId&gt;javax.servlet&lt;/groupId&gt; &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt; ... &lt;artifactId&gt;commons-codec&lt;/artifactId&gt; &lt;version&gt;1.10&lt;/ver

    commons-io等jar

    cog-jglobus-1.2.jar&lt;br&gt;cog-karajan-0.23.jar&lt;br&gt;cog-resources-1.0.jar&lt;br&gt;我所收集的一些jar包,其中包含:&lt;br&gt;cog-tomcat.jar&lt;br&gt;cog-util-0.91.jar&lt;br&gt;commons-io-1.3.1.jar&lt;br&gt;cryptix.jar&lt;br&gt;cryptix32.jar&lt;br&gt;...

Global site tag (gtag.js) - Google Analytics