Java are two ways to copy files

The first:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;

class the CopyFile {public
    public static void main (String [] args) throws IOException {
        Scanner sc = new new Scanner (System.in);
        System.out.println ( "Please enter the file name:"); // absolute console input file or relative path
        String sc.next S = ();
        
        // String S = "writer.txt";
        file file = new new file (S); // determine whether the file
        Boolean file.canExecute Boo = ();
        the InputStream = the FileInputStream new new IS (S);
        
        IF (== Boo to true) {
            
            String = file.getAbsolutePath STR () Replace ( "W", "copy_w");.
            //System.out.println(str);
            File = copyFile new file (str); // file copy
            copyFile.createNewFile ();
            
            OutputStream os = new FileOutputStream(str);
            int n = 0;
            while((n = is.read()) != -1){
                os.write(n);
            }
            
            System.out.println("复制成功");
            
            
        }
    }
}

The second:

java.io.File Import;
Import java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.FileOutputStream;
Import java.io.IOException;
Import java.util.Scanner;
/ **
 * Copy the pictures, because copy the same place so different file names;
 * 
 * @author L
 *
 * /
public class CopyFileTwo {

    static void main public (String [] args) throws IOException {
        Scanner Scanner new new SC = (the System.in);
        System.out.println ( "Please enter a file name"); // address of the file entry input console
        String s = sc.next ();
        file file = new new file (S);
        
        copyFile (file); // pass in the original document
                
    }
    public static void copyFile (file file) throws IOException {
        / *
         * function: the parameter file transfer go in, get the new file a name change completion
         * Next to copy the original content into a new file to
         * /
        file files = the Check (file);
        IF (! files = null) {
        FileInputStream fi = new new FileInputStream (file. getAbsoluteFile ());
        a FileOutputStream new new OS = a FileOutputStream (files.getAbsoluteFile ());
        = 0 n-int;
        the while ((n-fi.read = ()) = -1!) {
            os.write (n-);
        }
        fi.close ();
        os.close ();
        }
    }
    public static Check File (File file) throws IOException {
        / *
         * accepts a file, determine whether the document, if there is a return copy_ prefix plus empty file
         * If it does not null
         * /
        System.out.println (File.Exists ());
        IF (File.Exists ()) {
            String file.getParent STR = () + "\\ Copy_" + file.getName ();
            
            File = file2 new new File (STR);
            file2.createNewFile ();
            return file2;        
        }
        the else {
            System.out.println ( "not this file");
            return null;
        }
    }
    
    

}

 

 

 

Published 37 original articles · won praise 105 · Views 6853

Guess you like

Origin blog.csdn.net/S9264L/article/details/104956328