FileOutStream FileInputStream and simple to use instance;

// Create a file copy method 

public static void CopyFile (File the src, File The docfile) throws Exception { // create a file input stream the FileInputStream INPUT = new new the FileInputStream (the src); // a file output stream to create a FileOutputStream OUT = new new a FileOutputStream (the docfile); // the method STR // define a variable accepts read byte / * int = B -1; the while (! (B = input.read ()) = -. 1) { // the System. err.println ((char) b); // b to accept the character char is actually transformed into computer code is printed // read the bytes written to the destination file out.write (b); } * / // Method one end str Method Two If the file is larger buffer can also be set as follows; / * * The following is a set of cache compared to the upper method efficiency is much higher * compared to top approach a bit like a drop of water drop at a time drink less enjoyable plus buffer zone is like a cup that take full drinking cup is a bit below our array declarations; * * * / // define a buffer for each array is read into the cache first byte then collectively output efficiency region; byte [] B = new new byte [1024 ]; // for each receiving a number of bytes read; int len = -1 ; // read (byte []) reads a certain number of bytes is the size of the parameter set into the read buffer to return the number of bytes per read () to return each read byte; the while ((len = input.read (B)) =! -1 ) { // the output buffer of the byte to the destination file because the number of bytes read at the end of the file so each uncertain 0 byte length to the actual output of the read buffer; out.write (B, 0, len); } // because the input stream is used to shut the system stream level so that resource release resources; input.close (); the out.close (); }
    public  static  void main (String [] SAGE) {
         // to copy the source file 
        File File = new new File ( "E: \\ overtime records .txt" );
         // destination file path 
        String filePath = "E: \\ copy COPY3 copy2 \\ \\ " ; 
        file docParent = new new file (filePath);
         // determine the current file or folder exists; 
        IF (! docParent.exists ()) {
             // If the file does not exist to create a path to the directory directory 
           docParent. mkdirs ();
            // value to create a subdirectory such as: create copy3 copy2 directory must exist or do not create mkdirs () is to create all directories;
            // docParent.mkdir (); 
        }
         //Create an object file; 
        File = docfile new new File (filePath the File.separator + + "copy overtime records .txt" );
         the try {
             // perform copy method; 
            CopyFile (File, docfile); 
        } the catch (Exception E) { 
            e.printStackTrace (); 
        } 
    }

 

Guess you like

Origin www.cnblogs.com/wangfl/p/10981203.html