• org.apache.commons.io使用实例


    1.文件内容拷贝:
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.Writer;
     
    import org.apache.commons.io.FileUtils;
    import org.apache.commons.io.IOUtils;
     
     
    publicclass FileCopyExample {
        publicstaticvoid main(String[] args) {
           try {
               File src = new File( "test.dat" );
               File dest = new File( "test.dat.bak" );
              
               FileUtils.copyFile( src, dest );
           } catch( IOException ioe ) {
               System.out.println( "Problem copying file." );
           }
          
           try {
               File src = new File( "test.dat" );
               File dir = new File( "./temp" );
     
               FileUtils.copyFileToDirectory( src, dir );
           } catch( IOException ioe ) {
               System.out.println( "Problem copying file to dir.");
           }
          
           try {
               String string = "Blah blah blah";
               File dest = new File( "test.tmp" );
              
               FileUtils.writeStringToFile( dest, string, "ISO-8859-1" );
           } catch( IOException ioe ) {
               System.out.println( "Error writing out a String." );       
           }
          
          
           try {
               Writer writer = new FileWriter( "test.dat" );
               InputStream inputStream =FileCopyExample.class.
                  getClass().getResourceAsStream("/io/web.xml");
               IOUtils.copy( inputStream, writer );
               writer.close();
               inputStream.close();
           } catch (IOException e) {
               System.out.println( "Error copying data" );
           }
     
    try {
               File src = new File( "test.txt" );
               OutputStream output = new FileOutputStream(src);
               InputStream inputStream =FileCopyExample.class.
                  getClass().getResourceAsStream("/io/web.xml");
               IOUtils.copy( inputStream, output );
    //         writer.close();
               inputStream.close();
               output.close();
           } catch (IOException e) {
               System.out.println( "Error copying data" );
           }
        }
     
    }
     
    2.文件删除:
    File file = new File( ("io/project.properties") );
           String display = FileUtils.byteCountToDisplaySize( file.length() );
           System.out.println( "project.xml is " + display );
           FileUtils.forceDelete(file)
     
    3.读取取文本中的每一行:
    import java.io.File;
    import java.io.IOException;
    import java.util.List;
     
    import org.apache.commons.io.FileSystemUtils;
    import org.apache.commons.io.FileUtils;
     
     
    public class Test {
     
           /**
            * @param args
            * @throws IOException
            */
           public static void main(String[] args) throws IOException {
                  // TODO Auto-generated method stub
                  File file = new File((Test.class.getClass().getResource("/io/web.xml")).getFile());
                   List lines = FileUtils.readLines(file, "UTF-8");
                   for(int i=0;i<lines.size();i++)
                   System.out.println(lines.get(i));
           }
     

  • 相关阅读:
    Win7+Ubuntu11.10(EasyBCD硬盘安装)
    hdu 3661 Assignments
    hdu 1128 Self Numbers
    CF 152 B. Chilly Willy
    hdu 1754 I Hate It
    A survey of wireless network simulation and/or emulation software for use in higher education
    How to Fix Packet Loss: Causes, Simple Solutions & Top Tools
    getchar函数
    C++“左值”和“右值”
    stdio.h中的stdio
  • 原文地址:https://www.cnblogs.com/PatrickLee/p/2640010.html
Copyright © 2020-2023  润新知