Android Directory Copy CopyDir(String sourcedir, String destdir)

Here you can find the source of CopyDir(String sourcedir, String destdir)

Description

This class copies an input files of a directory to another directory not include subdir

Parameter

Parameter Description
String destdir the target directory

Declaration

public static void CopyDir(String sourcedir, String destdir)
        throws Exception 

Method Source Code

//package com.java2s;
import java.io.*;

public class Main {
    /**/*  w  w w  . j a  va2 s .c  om*/
     *  This class copies an input files of a directory to another directory not include subdir
     *
     *  @param String sourcedir   the directory to copy from such as:/home/bqlr/images
     *  @param String destdir      the target directory
     */
    public static void CopyDir(String sourcedir, String destdir)
            throws Exception {
        File dest = new File(destdir);
        File source = new File(sourcedir);

        String[] files = source.list();
        try {
            makehome(destdir);
        } catch (Exception ex) {
            throw new Exception("CopyDir:" + ex.getMessage());
        }

        for (int i = 0; i < files.length; i++) {
            String sourcefile = source + File.separator + files[i];
            String destfile = dest + File.separator + files[i];
            File temp = new File(sourcefile);
            if (temp.isFile()) {
                try {
                    copy(sourcefile, destfile);
                } catch (Exception ex) {
                    throw new Exception("CopyDir:" + ex.getMessage());
                }
            }
        }
    }

    /**
     * create a directory
     * @param home
     * @throws Exception
     */
    public static void makehome(String home) throws Exception {
        File homedir = new File(home);
        if (!homedir.exists()) {
            try {
                homedir.mkdirs();
            } catch (Exception ex) {
                throw new Exception("Can not mkdir :" + home
                        + " Maybe include special charactor!");
            }
        }
    }

    /**
     *  This class copies an input file to output file
     *
     *  @param String input file to copy from
     *  @param String output file
     */
    public static boolean copy(String input, String output)
            throws Exception {
        int BUFSIZE = 65536;
        FileInputStream fis = new FileInputStream(input);
        FileOutputStream fos = new FileOutputStream(output);

        try {
            int s;
            byte[] buf = new byte[BUFSIZE];
            while ((s = fis.read(buf)) > -1) {
                fos.write(buf, 0, s);
            }

        } catch (Exception ex) {
            throw new Exception("makehome" + ex.getMessage());
        } finally {
            fis.close();
            fos.close();
        }
        return true;
    }
}

Related

  1. copyDir(File source, File destination)
  2. copyDirectory(@NotNull String srcDir, @NotNull String dstDir, @NotNull final List excludes)
  3. copyDirectory(File from, File to)
  4. copyDirectory(File from, File to, byte[] buffer)