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( );
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 );
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 );
}
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 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 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 );
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);
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 );
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 );
File dir = new File( "temp" );
FileUtils.deleteDirectory( dir );
如果只想清空目录下所有内容,并不删除该目录,可以这样写:
FileUtils.cleanDirectory( dir );
很简单地,可以得到一个目录的大小:
File dir = new File( "temp" );
long dirSize = FileUtils.sizeOfDirectory( );
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 ) );
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 ));
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 ));