การคัดลอก Directory ใน Java

ต่อไปนี้คือตัวอย่างการคัดลอก directory ใน Java โดยจะทำการคัดลอก directory และข้อมูลหรือไฟล์ที่อยู่ในนั้นทั้งหมดไปยัง directory ใหม่ครับ.

ตัวอย่าง 
ทำการคัดลอกโฟรเดอร์ "C:\\users\\nopphanan7\\Directory1" และไฟล์ที่อยู่ข้างในไปยังโฟรเดอร์ใหม่ที่ชื่อ "C:\\users\\nopphanan7\\Directory1-new".
package demo.directory;

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

public class CopyDirectoryExample {

    /**
     * @param args
     */
    public static void main(String[] args) {
        File srcFolder = new File("C:\\users\\nopphanan7\\Directory1");
        File destFolder = new File("C:\\users\\nopphanan7\\Directory1-new");

        // make sure source exists
        if (!srcFolder.exists()) {

            System.out.println("Directory does not exist.");
            // just exit
            System.exit(0);
        } else {
            try {
                copyFolder(srcFolder, destFolder);
            } catch (IOException e) {
                e.printStackTrace();
                // error, just exit
                System.exit(0);
            }
        }
        System.out.println("Done");
    }

    public static void copyFolder(File src, File dest) throws IOException {

        if (src.isDirectory()) {

            // if directory not exists, create it
            if (!dest.exists()) {
                dest.mkdir();
                System.out.println("Directory copied from " + src + "  to "
                        + dest);
            }

            // list all the directory contents
            String files[] = src.list();

            for (String file : files) {
                // construct the src and dest file structure
                File srcFile = new File(src, file);
                File destFile = new File(dest, file);
                // recursive copy
                copyFolder(srcFile, destFile);
            }

        } else {
            // if file, then copy it
            // Use bytes stream to support all file types
            InputStream in = new FileInputStream(src);
            OutputStream out = new FileOutputStream(dest);

            byte[] buffer = new byte[1024];

            int length;
            // copy the file content in bytes
            while ((length = in.read(buffer)) > 0) {
                out.write(buffer, 0, length);
            }

            in.close();
            out.close();
            System.out.println("File copied from " + src + " to " + dest);
        }
    }
}
ผลลัพธ์ที่ได้คือ
Directory copied from C:\users\nopphanan7\Directory1  to C:\users\nopphanan7\Directory1-new
File copied from C:\users\nopphanan7\Directory1\photo.jpg to C:\users\nopphanan7\Directory1-new\photo.jpg
File copied from C:\users\nopphanan7\Directory1\photo2.jpg to C:\users\nopphanan7\Directory1-new\photo2.jpg
File copied from C:\users\nopphanan7\Directory1\the_dark_knight.jpg to C:\users\nopphanan7\Directory1-new\the_dark_knight.jpg
File copied from C:\users\nopphanan7\Directory1\ubuntu-black-1440x900.jpg to C:\users\nopphanan7\Directory1-new\ubuntu-black-1440x900.jpg
Done

About Nop

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment