Java - Hướng dẫn tạo ứng dụng copy file bằng thư viện commons-io của Apache.(Copy file with Apache Lib).




Bài viết hôm nay sẽ hướng dẫn cách copy một tập tin bằng cách sử dụng gói thư viện common-io của Apache.

Đầu tiên ta sẽ tìm hiểu Apache là gì? Tôi cũng chả biết nó là gì ^^! Thôi thì nghe nó tự giới thiệu vậy.






Chúng tôi không chỉ là một nhóm các dự án chia sẻ dịnh vụ, mà chúng tôi là một cộng đồng các nhà phát triển và người sử dụng.

Apache Software Foundation(nền tảng phần mềm Apache) hỗ trợ cung cấp các dự án mã nguồn mở của Apache để phục vụ cho lợi ích chung của công đồng các nhà phát triển và người sử dụng.

Dài dòng quá hiểu đơn giản nó là một công đồng của người phát triển và người sử dụng, trong đó người phát triển sẽ cung cấp các mã nguồn mở cho người sử dụng, còn người sử dụng thì hỗ trợ ngược lại cho người phát triển bằng cách sử dụng, kiểm tra, đóng góp ý kiến, cung cấp cấp ý tưởng, giải pháp... thậm chí là money(cái này không rõ ^^!). Nói chung là đôi bên cùng có lợi và cùng đóng góp xây dựng mã nguồn mở một cách tốt nhất.

Bây giờ vào chủ để chính, để thực hiện bài này trước tiên ta cần download bộ thư viện(library) commons-io của Apache, bạn có thể download tại đây Apache Commons IO.

Dưới đây hàm sử dụng Apache Commons IO để sao chép tập tin:
private static void copyFileUsingApacheCommonsIO(File source, File dest) throws IOException
{
    FileUtils.copyFile(source, dest);
}

Đây là toàn bộ mã để thực hiện việc copy tập tin:
/**
 * @(#)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;

import org.apache.commons.io.FileUtils;

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);
            copyFileUsingApacheCommonsIO(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();
        }
    }
   
    private static void copyFileUsingApacheCommonsIO(File source, File dest) throws IOException {
        FileUtils.copyFile(source, dest);
    }       
   
}













No comments:

Post a Comment