VietTuts

Tự Học Lập Trình Online

  • Home
  • Java
  • Servlet
  • JSP
  • Struts2
  • Hibernate
  • Spring
  • MyBatis
  • Java WS
  • C
  • C++
  • C#
  • Python
  • PHP
  • Excel
  • VBA
  • Web
    • JavaScript
    • JQUERY
    • JSON
    • AJAX
    • CSS
    • HTML
    • HTML5
    • Node.js
    • Angular 7
  • SQL
    • MySQL
    • SQL Server
  • Misc
    • Eclipse
    • Phần mềm tiện ích
    • Cấu trúc DL&GT
    • Selenium Test
Java Cơ Bản Các Khái Niệm Java OOPs Java String Java I/O

Ví dụ Java I/O

Java Input/Output Các ví dụ về Java I/O

Thao Tác Với File Trong Java

Tạo file trong java Đọc file với BufferedInputStream trong java Đọc file với BufferedReader trong java Đọc ghi file trong java Append nội dung vào file trong java Delete file trong java Delete nhiều file dựa vào phần mở rộng trong java Tìm các file dựa vào phần mở rộng trong java Đổi tên file trong java Copy file trong java Move file trong java Check file tồn tại trong java Check file ẩn trong java Liệt kê tất cả file và folder trong một folder Get thư mục hiện tại trong Java Read properties file trong java

Thao Tác Với Folder Trong Java

Check thư mục rỗng trong java Tạo thư mục trong java Xóa thư mục trong java Copy thư mục trong java

ZIP File Trong Java

ZIP file trong java
Xử Lý Ngoại Lệ Các Lớp Lồng Nhau Đa Luồng (Multithreading) Java AWT Java Swing Lập Trình Mạng Với Java Java Date Chuyển Đối Kiểu Dữ Liệu Java Collections Java JDBC Các Tính Năng Mới Trong Java Bài Tập Java Có Lời Giải Câu Hỏi Phỏng Vấn Java
1 / 3
❮ ❯

Copy thư mục trong java


Xóa thư mục trong java
ZIP file trong java

Không có phương thức copy thư mục nào sẵn có trong java. Để copy một thư mục trong java từ vị trí này sang vị trí khác với tất cả các thư mục con và các file mà chúng chứa thì chúng ta phải copy từng thư mục con và file sang vị trí mới.

Nội dung chính

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

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

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

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:\tempNew
Thu muc da duoc tao D:\tempNew\java
Thu muc da duoc tao D:\tempNew\java\java-core
File da duoc copy D:\tempNew\java\java-core\javacore1.txt
Thu muc da duoc tao D:\tempNew\src
File da duoc copy D:\tempNew\src\test1.txt
File da duoc copy D:\tempNew\test2.txt
File da duoc copy D:\tempNew\test3.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

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:\tempNew
Thu muc da duoc tao D:\tempNew\java
Thu muc da duoc tao D:\tempNew\java\java-core
File da duoc copy D:\tempNew\java\java-core\javacore1.txt
Thu muc da duoc tao D:\tempNew\src
File da duoc copy D:\tempNew\src\test1.txt
File da duoc copy D:\tempNew\test2.txt
File da duoc copy D:\tempNew\test3.txt

Xóa thư mục trong java
ZIP file trong java

Recent Updates

Xuất dữ liệu ra màn hình console trong JavaCài đặt môi trường JavaJava Swing - Bài tập quản lý sinh viên trong javaLinkedList trong javaArrayList trong javaBài tập java có lời giảiXác thực dữ liệu (Data Validation) trong ExcelGiới thiệu các hàm có sẵn trong ExcelSheet Options trong ExcelHướng dẫn lập trình Python với PyCharm Community EditionHướng dẫn lập trình Python với Visual Studio CodeGiới thiệu CSS3Sắp Tết 2026 Rồi! - Còn bao nhiêu ngày nữa là đến tết 2026?

VietTuts on facebook

Học Lập Trình Online Miễn Phí - VietTuts.Vn
Danh Sách Bài Học

Học Java | Hibernate | Spring
Học Excel | Excel VBA
Học Servlet | JSP | Struts2
Học C | C++ | C#
Học Python
Học SQL

Bài Tập Có Lời Giải

Bài tập Java
Bài tập C
Bài tập C++
Bài tập C#
Bài tập Python
Ví dụ Excel VBA

Câu Hỏi Phỏng Vấn

201 câu hỏi phỏng vấn java
25 câu hỏi phỏng vấn servlet
75 câu hỏi phỏng vấn jsp
52 câu hỏi phỏng vấn Hibernate
70 câu hỏi phỏng vấn Spring
57 câu hỏi phỏng vấn SQL

Scroll back to top

Copyright © 2016 VietTuts.Vn all rights reserved. | Liên hệ | Chính sách - riêng tư | sitemap.html | sitemap_index.xml