妙用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的笔录下来
分享到:
相关推荐
<artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> </...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
添加依赖: <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext7-core... <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency>
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
<artifactId>commons-httpclient</artifactId> <version>3.0</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </...
<version>${commons-lang3.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.spring...
<artifactId>commons-collections4</artifactId> <version>4.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </...
struts-1.3.8 包(<br><br>antlr-2.7.2.jar<br>bsf-2.3.0.jar<br>commons-beanutils-1.7.0.jar<br>commons-chain-1.1.jar<br>commons-digester-1.8.jar<br>commons-fileupload-1.1.1.jar<br>commons-io-1.1.jar<br>...
其中包含:shale-core.jar<br>commons-beanutils.jar<br>commons-chain.jar<br>commons-codec.jar<br>commons-collections.jar<br>commons-digester.jar<br>commons-el.jar<br>commons-fileupload.jar<br>commons-...
<artifactId>standard</artifactId> <version>1.1.2</version> <artifactId>jstl</artifactId> ... <artifactId>commons-io</artifactId> <version>2.11.0</version> <artifactId>javax.servlet-api</artifactId>
<artifactId>commons-io</artifactId> <version>2.11.0</version> <artifactId>mybatis</artifactId> <version>3.5.5</version> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> ...
<module>cloud-api-commons</module> <module>cloud-eureka-server7001</module> <module>cloud-eureka-server7002</module> <module>cloud-comsumer-order80</module> <module>cloud-provider-payment8001</...
collections-2.1.1.jar<br>commons-logging-1.0.4.jar<br>dom4j-1.6.1.jar<br>ehcache-1.1.jar<br>hibernate3.jar<br>jaas.jar<br>jaxen-1.1-beta-7.jar<br>jdbc2_0-stdext.jar<br>jta.jar<br>log4j-1.2.11.jar<br>...
api.jar<br>jxl.jar<br>itext-1.3.jar<br>poi-2.5.1.jar<br>cos.jar<br>velocity-1.5.jar<br>velocity-tools-view-1.3.jar<br>commons-codec.jar<br>antlr.jar<br>commons-beanutils.jar<br>commons-digester.jar<br...
ssm在maven项目管理下的pom文件,jar包依赖 <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> ... <artifactId>commons-codec</artifactId> <version>1.10</ver