Application of the input output streams

1 File Copy

Copy the files to achieve with FileInputStream and FileOutPutStream, this method does not copy the folder.

package pers.zhh.copy;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyDemo {
    public static void main(String[] args) throws IOException {
        FileInputStream fi = new FileInputStream("M:\\数据库.zip");
        FileOutputStream fo = new FileOutputStream("M:\\数据库1.zip");
        int num = 0;
        long startTime = System.currentTimeMillis();
        the while (! (NUM = fi.read ()) = - . 1 ) { 
            fo.write (NUM); 

        } 
        fo.close (); 
        fi.close (); 
        Long endTime = System.currentTimeMillis (); 
        the System. OUT . the println ( " perform this procedure with the " + (endTime - the startTime) + " msec. " ); 
    } 
}

(2) an array of buffer:

package pers.zzz.copy;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyDemo1 {
    public static void main(String[] args) throws IOException {
        FileInputStream fi = new FileInputStream("M:\\网页设计.zip");
        FileOutputStream fo = new FileOutputStream("M:\\网页设计3.zip");
        byte[] buf = new byte[1024];
        int len = 0 ;
         Long the startTime = System.currentTimeMillis ();
         the while ((len = fi.read (buf)) = -! . 1 ) { 
            fo.write (buf, 0 , len); // the specified data array length It is written to the output stream. 
        } 
        Fo.close (); 
        fi.close (); 
        Long endTime = System.currentTimeMillis (); 
        the System. OUT .println ( " perform this procedure with the " + (endTime - the startTime) + " msec. " ); 
    } 

}

In the first method, one can only read one byte of data, copy data only a few M took 7s time, very low efficiency. While the second by way of a buffer array, copy close 1G file only spent 4s time, greatly enhance the efficiency.

2, IO stream exception handling

package pers.zzz.copy;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyDemo1 {
    public static void main(String[] args) {
        FileOutputStream fo = null;
        FileInputStream fi = null;
        long startTime = System.currentTimeMillis();
        try {
            fi = new FileInputStream("M:\\网页设1计.zip");
            fo = newA FileOutputStream ( " M: \\ web design 11.zip " ); 

            byte [] = buf new new  byte [ 1024 ];
             int len = 0 ; 

            the while (! (Len = fi.read (buf)) = - . 1 ) { 
                FO .write (buf, 0 , len); // write data length specified in the array to the output stream. 
            } 
        } The catch (IOException E) { 
            the System. OUT .println (e.toString ()); 
        } the finally {
             IF (! FO = null ) {
                try {
                    fo.close();
                } catch (IOException e) {
                    System.out.println(e.toString());
                    throw new RuntimeException();
                }
            }
            if (fi != null) {
                try {
                    fi.close();
                } catch (IOException e) {
                    System.out.println(e.toString());
                    throw new RuntimeException();

                }

            } 

        } 
        Long endTime = System.currentTimeMillis (); 

        . The System OUT .println ( " perform this procedure with the " + (endTime - the startTime) + " msec. " ); 
    } 
}

 

Guess you like

Origin www.cnblogs.com/zhai1997/p/11366386.html