Java - Hướng dẫn tạo ứng dụng copy file bằng lớp Java NIO(Copy file with Java NIO).



Bài viết hôm nay sẽ hướng dẫn cách sao chép một tập tin bằng lớp Java NIO(Java NIO Class). Đầu tiên ta cần tìm hiểu khái miện về lớp NIO.

Các lớp Java NIO đã được giới thiệu trong Java 1.4 và FileChanell, nó được sử dụng để sao chép một tập tin trong java. Theo Phương pháp transferFrom(), đây là cách để sao chép tập tin được cho là nhanh hơn so với sử dụng Streams để sao chép tập tin.





Nhưng theo đánh giá của các lập trình viên phân tích trên Internet thì nó chỉ nhanh hơn đối với các tập tin có dung lượng vừa và nhỏ, còn đối với dung lượng lớn thì Streams vẫn là tốt nhất.

Bên dưới là mã nguồn của một phương thức sử dụng Java NIO:
private static void copyFileUsingChannel(File source, File dest) throws IOException {
    FileChannel sourceChannel = null;
    FileChannel destChannel = null;
    try {
        sourceChannel = new FileInputStream(source).getChannel();
        destChannel = new FileOutputStream(dest).getChannel();
        destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
       }finally{
           sourceChannel.close();
           destChannel.close();
       }
}

Như trong đoạn mã trên bạn thấy tập tin nguồn(source) và tập tin đính(dest) được chuyển thành các kênh tập tin(FileChannel) và sử dụng hàm transferFrom() để chuyển dữ liệu của tập tin nguồi sang tập tin đính.

Nó đới giản hơn so với dùng Stream phải không vì Stream ta phải viết vòng For hoặc While để thực hiện việc chuyển dữ liệu còn NIO thì đơn là dùng hàm transferFrom.

Nếu bạn chưa biết về Stream thì có thể tham khảo bài viết ở liên kết bên dưới.

http://blog.vnlives.net/2013/08/java-tao-ung-dung-sao-chep-tap-tin-bang.html

Sau đây là toàn bộ mã nguồn của bài này.
/**
 * @(#)CopyFileBasic.java
 *
 * CopyFileBasic application
 *
 * @author VNLIVES.NET
 * @version 1.00 2013/8/23
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;

public class CopyFileBasic {
   
    public static void main(String[] args) {
        // TODO, add your application code
        try {
            File fa = new File("Path File A");
            File fb = new File("Path File B");           
            //copyFileUsingStream(fa, fb);
            copyFileUsingChannel(fa, fb);
        } catch (IOException io) {
           
        }    

    }
   
    private static void copyFileUsingChannel(File source, File dest) throws IOException {
        FileChannel sourceChannel = null;
        FileChannel destChannel = null;
        try {
            sourceChannel = new FileInputStream(source).getChannel();
            destChannel = new FileOutputStream(dest).getChannel();
            destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
           }finally{
               sourceChannel.close();
               destChannel.close();
           }
    }   
   
    private static void copyFileUsingStream(File source, File dest) throws IOException {
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(source);
            os = new FileOutputStream(dest);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = is.read(buffer)) > 0) {
                os.write(buffer, 0, length);
            }
        } finally {
            is.close();
            os.close();
        }
    }   
   
}











No comments:

Post a Comment