分享 commons io 工具类 代码
commons io
输入流复制到输出流
public class IoTest {/** * @param args */public static void main(String[] args) throws Exception {// TODO Auto-generated method stubWriter write = new FileWriter("c:\\kk.dat");InputStream ins = new FileInputStream(new File("c:\\text.txt"));IOUtils.copy(ins, write);write.close();ins.close();}}文本写入指定文件
public class FileWirterTest {/** * @param args */public static void main(String[] args) throws Exception{// TODO Auto-generated method stubString name = "my name is panxiuyan";File file = new File("c:\\name.txt");FileUtils.writeStringToFile(file, name);}}将输入流转换成文本
public class URLIoTest {/** * @param args */public static void main(String[] args) throws Exception {// TODO Auto-generated method stubURL url = new URL("http://www.dimurmill.com");InputStream ins = url.openStream();String contents = IOUtils.toString(ins,"UTF-8"); System.out.println( "Slashdot: " + contents );}}关闭相关流
public class IoCloseTest {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubFile file = null;InputStream ins = null;try{file = new File("C:\\Test.java");ins = new FileInputStream(file);}catch(Exception e){e.printStackTrace();}finally{IOUtils.closeQuietly(ins);}}}文件复制
public class FileCopyTest {/** * @param args */public static void main(String[] args) throws Exception{// TODO Auto-generated method stubFile srcfile = new File("c:\\Test.java");File destfile = new File("c:\\Test.java.bak");FileUtils.copyFile(srcfile, destfile);}}文件复制指定的目录
public class FileCopyTest {/** * @param args */public static void main(String[] args) throws Exception{// TODO Auto-generated method stubFile srcfile = new File("c:\\Test.java");File destDir = new File("D:\\");FileUtils.copyFileToDirectory(srcfile, destDir);}}网络流保存为文件
public class URLToFileTest {/** * @param args */public static void main(String[] args) throws Exception{// TODO Auto-generated method stubURL url = new URL("http://www.163.com");File file = new File("c:\\163.html");FileUtils.copyURLToFile(url, file);}}文件目录操作
public class DirOper {/** * @param args */public static void main(String[] args) throws Exception {// TODO Auto-generated method stubFile dir = new File("c:\\test");FileUtils.cleanDirectory(dir);//清空目录下的文件FileUtils.deleteDirectory(dir);//删除目录和目录下的文件}}目录大小
long size = FileUtils.sizeOfDirectory(dir);
目录操作
File testFile = new File( "testFile.txt" ); //如果不存在,新建 // 如果存在,修改文件修改时间 FileUtils.touch( testFile );
记录流的读取写入字节数
File test = new File( "test.dat" );//输出流的统计CountingOutputStream countStream = null;//输入流的统计//CountingInputStream countStream = null;try { FileOutputStream fos = new FileOutputStream( test ); countStream = new CountingOutputStream( fos ); countStream.write( "Hello".getBytes( ) );} catch( IOException ioe ) { System.out.println( "Error writing bytes to file." );} finally { IOUtils.closeQuietly( countStream );}if( countStream != null ) { int bytesWritten = countStream.getCount( ); System.out.println( "Wrote " + bytesWritten + " bytes to test.dat" );}相同的内容写入不同的文本
File test1 = new File("split1.txt");File test2 = new File("split2.txt");OutputStream outStream = null; try { FileOutputStream fos1 = new FileOutputStream( test1 ); FileOutputStream fos2 = new FileOutputStream( test2 ); //包含不同的文本 outStream = new TeeOutputStream( fos1, fos2 ); outStream.write( "One Two Three, Test".getBytes( ) ); outStream.flush( );} catch( IOException ioe ) { System.out.println( "Error writing to split output stream" );} finally { IOUtils.closeQuietly( outStream );} 1 楼 zhzhy86 2010-01-22 不错,学习了 2 楼 毕竟红尘 2010-01-22 不错啊我已经将整个Apache Common包里面提供的工具,当作项目的基础组件了。
自己积累下来的许多工具组件,在后来发觉Common 包里面都已经有了...... 3 楼 gongmingwind 2010-01-22 确实不错,最近也在学commons ,有机会大家多交流啊 4 楼 odd2008 2010-01-22 请问楼主,最后一段代码,
IOUtils.closeQuietly( outStream );
只关闭输出流,不关闭输入流吗? 5 楼 aoliwen521 2010-01-22 请问这个包是jfc的吗?还是要引入其他的包? 6 楼 zhangyou1010 2010-01-22 把一些循环读和写流封装而已。 7 楼 chenhongwei0924 2010-12-08
自己积累几个工具类,不经意..发觉Common 包里面都已经有了...... 8 楼 kongzhizhen 2011-01-05 zhangyou1010 写道把一些循环读和写流封装而已。
工具类,偶理解的工具类就是封装一些常用的操作。没什么不妥。