Java文件複製實現

一、引言

文件複製是計算機操作中常見的應用場景之一,也是Java編程中的常見問題。文件複製的實現有多種方式,Java提供了多種API,如io、nio、Channel等,本文將介紹如何使用Java API來實現文件複製。

二、Java IO 文件複製

1. 文件讀取

在進行文件複製之前,需要先將源文件讀取到內存中。Java IO提供了文件讀取相關的API,最常見的就是使用FileInputStream來讀取一個文件。

    FileInputStream fis = null;
    byte[] buffer = new byte[1024];
    try {
        fis = new FileInputStream(srcFile);
        int len;
        while ((len = fis.read(buffer)) != -1) {
            //將文件信息讀取到緩存中
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

2. 文件寫入

文件讀取到緩存中後,需要將其寫入到目標文件中。Java IO提供了文件寫入相關的API,最常見的就是使用FileOutputStream來寫入一個文件。

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(destFile);
        fos.write(buffer, 0, len);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

3. 文件複製

將文件讀取和寫入組合在一起,即可實現文件複製操作。

    public static void copyFile(String srcPath, String destPath){
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);

        FileInputStream fis = null;
        FileOutputStream fos = null;
        byte[] buffer = new byte[1024];
        try {
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);
            int len;
            while ((len = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

三、Java NIO 文件複製

Java NIO是Java IO的進階版,提供更高效的I/O操作和更靈活的文件處理方式。在Java NIO中,文件操作主要基於Channel和Buffer。

1. 文件讀取

使用Java NIO進行文件讀取,需要使用FileChannel來代替FileInputStream。

    FileInputStream fis = null;
    FileChannel fcin = null;
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    try {
        fis = new FileInputStream(srcFile);
        fcin = fis.getChannel();
        while (fcin.read(buffer) != -1) {
            buffer.flip();
            //將文件信息讀取到緩存中
            buffer.clear();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            fcin.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

2. 文件寫入

使用Java NIO進行文件寫入,需要使用FileChannel來代替FileOutputStream。

    FileOutputStream fos = null;
    FileChannel fcout = null;
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    try {
        fos = new FileOutputStream(destFile);
        fcout = fos.getChannel();
        buffer.put(byteBuffer).flip();
        fcout.write(buffer);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            fcout.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

3. 文件複製

將文件讀取和寫入組合在一起,即可實現文件複製操作。

    public static void copyFileByNIO(String srcPath, String destPath) {
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);

        FileInputStream fis = null;
        FileOutputStream fos = null;
        FileChannel fcin = null;
        FileChannel fcout = null;
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        try {
            fis = new FileInputStream(srcFile);
            fcin = fis.getChannel();
            fos = new FileOutputStream(destFile);
            fcout = fos.getChannel();
            while (fcin.read(buffer) != -1) {
                buffer.flip();
                fcout.write(buffer);
                buffer.clear();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fcin.close();
                fis.close();
                fcout.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

四、結論

本文主要介紹了在Java中實現文件複製的兩種常見方式,一種是使用Java IO進行文件讀取和寫入,另一種是使用Java NIO進行文件讀取和寫入。兩種方式各自有其優缺點,具體使用時需要根據情況進行選擇。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/238853.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 12:13
下一篇 2024-12-12 12:13

相關推薦

發表回復

登錄後才能評論