Bài 211: Copy thư mục trong java

Ngày đăng: 1/6/2023 10:38:41 AM

Ví dụ về copy thư mục trong java

Ví dụ 1: sử dụng java.nio.file.Files.copy()

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50      

51

52

53

54

55

import java.io.File;

import java.io.IOException;

import java.nio.file.Files;

import java.nio.file.StandardCopyOption;

 

public class CopyFolderExample2 {

 

    /**

     * main

     *

     * @param args

     * @throws IOException

     */

    public static void main(String[] args) throws IOException {

        // sourceFolder: thu muc nguon duoc copy

        File sourceFolder = new File("D:\temp");

        // targetFolder: thu muc dich duoc copy den

        File targetFolder = new File("D:\tempNew");

        // goi phuong thuc copy

        copyFolder(sourceFolder, targetFolder);

    }

 

    /**

     * copy folder

     *

     * @param sourceFolder

     * @param targetFolder

     * @throws IOException

     */

    private static void copyFolder(File sourceFolder, File targetFolder)

            throws IOException {

        // Check neu sourceFolder la mot thu muc hoac file

        // neu sourceFolder la file thi copy den thu muc dich

        if (sourceFolder.isDirectory()) {

            // Xac nhan neu targetFolder chua ton tai thi tao moi

            if (!targetFolder.exists()) {

                targetFolder.mkdir();

                System.out.println("Thu muc da duoc tao " + targetFolder);

            }

            // Liet ke tat ca cac file va thu muc trong sourceFolder

            String files[] = sourceFolder.list();

            for (String file : files) {

                File srcFile = new File(sourceFolder, file);

                File tarFile = new File(targetFolder, file);

                // goi lai phuong thuc copyFolder

                copyFolder(srcFile, tarFile);

            }

        } else {

            // copy file tu thuc muc nguon den thu muc dich

            Files.copy(sourceFolder.toPath(), targetFolder.toPath(),

                    StandardCopyOption.REPLACE_EXISTING);

            System.out.println("File da duoc copy " + targetFolder);

        }

    }

}

Output:

 Thu muc da duoc tao D:	empNew
 Thu muc da duoc tao D:	empNewjava
 Thu muc da duoc tao D:	empNewjavajava-core
 File da duoc copy D:	empNewjavajava-corejavacore1.txt
 Thu muc da duoc tao D:	empNewsrc
 File da duoc copy D:	empNewsrc	est1.txt
 File da duoc copy D:	empNew	est2.txt
 File da duoc copy D:	empNew	est3.txt

Lưu ý: Sử dụng phương thức java.nio.file.Files.copy() có thể copy được thư mục. Tuy nhiên các file bên trong thư mục không được copy. Vì vậy cần phải tạo ra thư mục mới bằng lệnh targetFolder.mkdir();

Ví dụ 2: sủ dụng FileInputStream và FileOutputStream

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21     

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

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 CopyFolderExample1 {

    /**

     * main

     *

     * @param args

     * @throws IOException

     */

    public static void main(String[] args) throws IOException {

        // sourceFolder: thu muc nguon duoc copy

        File sourceFolder = new File("D:\temp");

        // targetFolder: thu muc dich duoc copy den

        File targetFolder = new File("D:\tempNew");

        // goi phuong thuc copy

        copyFolder(sourceFolder, targetFolder);

    }

 

    /**

     * copy folder

     *

     * @param sourceFolder

     * @param targetFolder

     * @throws IOException

     */

    private static void copyFolder(File sourceFolder, File targetFolder)

            throws IOException {

        // Check neu sourceFolder la mot thu muc hoac file

        // neu sourceFolder la file thi copy den thu muc dich

        if (sourceFolder.isDirectory()) {

            // Xac nhan neu targetFolder chua ton tai thi tao moi

            if (!targetFolder.exists()) {

                targetFolder.mkdir();

                System.out.println("Thu muc da duoc tao " + targetFolder);

            }

            // Liet ke tat ca cac file va thu muc trong sourceFolder

            String files[] = sourceFolder.list();

            for (String file : files) {

                File srcFile = new File(sourceFolder, file);

                File tarFile = new File(targetFolder, file);

                // goi lai phuong thuc copyFolder

                copyFolder(srcFile, tarFile);

            }

        } else {

            // copy file tu thuc muc nguon den thu muc dich

            InputStream in = new FileInputStream(sourceFolder);

            OutputStream out = new FileOutputStream(targetFolder);

            byte[] buffer = new byte[1024];

            int length;

            while ((length = in.read(buffer)) > 0) {

                out.write(buffer, 0, length);

            }

            System.out.println("File da duoc copy " + targetFolder);

            in.close();

            out.close();

        }

    }

}

Output:

 Thu muc da duoc tao D:	empNew
 Thu muc da duoc tao D:	empNewjava
 Thu muc da duoc tao D:	empNewjavajava-core
 File da duoc copy D:	empNewjavajava-corejavacore1.txt
 Thu muc da duoc tao D:	empNewsrc
 File da duoc copy D:	empNewsrc	est1.txt
 File da duoc copy D:	empNew	est2.txt
 File da duoc copy D:	empNew	est3.txt

Nguồn tin: viettuts.vn