分享 commons io 工具类 代码

来自:http://www.iteye.com/topic/575996

输入流复制到输出流


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

}

你可能感兴趣的:(java,C++,c,C#)