论坛首页 Java企业应用论坛

分享 commons io 工具类 代码

浏览 8463 次
精华帖 (0) :: 良好帖 (11) :: 新手帖 (9) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-01-20  
commons io

输入流复制到输出流

public class IoTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		Writer 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 stub
		
		String 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 stub
		URL 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 stub
		
		File 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 stub
		
		File 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 stub
		
		File 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 stub
		
		URL 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 stub

		File 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 );

}
   发表时间:2010-01-22  
不错啊
我已经将整个Apache Common包里面提供的工具,当作项目的基础组件了。
自己积累下来的许多工具组件,在后来发觉Common 包里面都已经有了......
0 请登录后投票
   发表时间:2010-01-22  
确实不错,最近也在学commons ,有机会大家多交流啊
0 请登录后投票
   发表时间:2010-01-22  
请问楼主,最后一段代码,
IOUtils.closeQuietly( outStream );  
只关闭输出流,不关闭输入流吗?
0 请登录后投票
   发表时间:2010-01-22  
请问这个包是jfc的吗?还是要引入其他的包?
0 请登录后投票
   发表时间:2010-01-22  
把一些循环读和写流封装而已。
0 请登录后投票
   发表时间:2010-12-08  

自己积累几个工具类,不经意..发觉Common 包里面都已经有了......
0 请登录后投票
   发表时间:2011-01-05  
zhangyou1010 写道
把一些循环读和写流封装而已。


工具类,偶理解的工具类就是封装一些常用的操作。没什么不妥。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics