How to copy a directory recursively , or a file , in java , or android ?

 

One way of recursively copying a directory , or copying a file , is by using the java .nio api . This also works , with android version 8 , or higher .

import static java .lang .System .out;

import java .nio .file .Paths;
import java .nio .file .Path;
import java .nio .file .Files;

import java .nio .file .DirectoryStream;

import java .nio .file .StandardCopyOption;
import java .nio .file .LinkOption;

import java .io .IOException;

public class Copy {
    public static final int final_abnormal_exit
            = 1 ;

    public static enum CopyFile_Options
            {overWriteN_copyFile , overWriteN_copyDirectory , undefined };

    public static void
            main (String args [ ] ){
        if (args
                .length != 2 )
            System .exit
                    (final_abnormal_exit );

        Path sourcePath = Paths .get (args [0 ] );
        Path destPath = Paths .get (args [1 ] );

        switch (makeDecision
                (sourcePath , destPath ) ){
            case overWriteN_copyFile :
                overWriteN_CopyFile (sourcePath, destPath );
                break;
            case overWriteN_copyDirectory :
                overWriteN_CopyDirectory (sourcePath, destPath );
                break;
            default :
                out .println ("undefined option selected "); }}


    public static CopyFile_Options
            makeDecision(Path sourcePath , Path destPath ){
        if ((Files .isRegularFile
                        (sourcePath , LinkOption .NOFOLLOW_LINKS )
                || Files .isSymbolicLink
                            (sourcePath ) )
                && Files .notExists
                            (destPath , LinkOption .NOFOLLOW_LINKS ) )
            return CopyFile_Options .overWriteN_copyFile ;

        if (Files .isDirectory
                        (sourcePath , LinkOption .NOFOLLOW_LINKS )
                && Files .notExists
                        (destPath , LinkOption .NOFOLLOW_LINKS ) )
            return CopyFile_Options .overWriteN_copyDirectory;

        return CopyFile_Options.undefined; }


    public static void
            overWriteN_CopyFile (Path sourcePath , Path destPath ){
        try{
            Files .copy
                    (sourcePath , destPath ,
                    LinkOption .NOFOLLOW_LINKS  ,
                    StandardCopyOption .COPY_ATTRIBUTES );
            /* Files .copy by default :
                 Does not override destination
                   files or directories .
                 If source is a link , it will copy its
                   target to destination .
                 It does not copy the source attributes .
               If source is a directory , its content are
                 not copied .*/ }
        catch (UnsupportedOperationException |
                IOException |
                SecurityException exception ){
            out .println (exception );
                    /* Files .copy also throws
                         DirectoryNotEmptyException and FileAlreadyExistsException ,
                         exceptions . They are caught using IOException ,
                         so it is not necessary , to catch these
                         exceptions .*/}}

    public static void
            overWriteN_CopyDirectory (Path sourcePath , Path destPath ){

        overWriteN_CopyFile (sourcePath , destPath );
        /*On entry copy , if directory , empty directory
          created , if file , file is copied .*/

        if (Files .isDirectory
                (sourcePath , LinkOption .NOFOLLOW_LINKS ) ){
            try(DirectoryStream <Path > paths =
                    Files .newDirectoryStream
                            (sourcePath ) ){
                for (Path tc_path : paths ){
                    Path newDesPath = destPath .resolve(tc_path .getFileName ( ) );
                    // s/a -> d -> d/a

                    if (Files .isDirectory
                            (tc_path , LinkOption .NOFOLLOW_LINKS ) ){
                        overWriteN_CopyDirectory (tc_path , newDesPath ); }
                    else
                       overWriteN_CopyFile (tc_path , newDesPath ); }}

            catch (IOException | SecurityException exception ){
                /*Files .newDirectoryStream also throws
                    NotDirectoryException , this can be
                    catched using IOException .*/
                out .println (exception ); } }}}
/*
$ javac Copy.java 
$ java Copy source destination */