Java IO 文件拷貝

粗糙版

package 學習資料.IO流;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class _5_1_文件拷貝 {
    public static void main(String[] args) {
        byte[] path = FiletoByte("dext.txt");
        BytetoFile("a.txt",path);
    }
    public static byte[] FiletoByte(String filepath) {
        File src = new File(filepath);                                           //建立文件對象

        InputStream is = null;                                                 //字節輸入流
        ByteArrayOutputStream baos = null;                                        //字節操作流

        try {
            is = new FileInputStream(src);                                        //打開文件對象
            baos = new ByteArrayOutputStream();                                        //獲得操作對象

            byte[] flush = new byte[1024*10];                                      //字節數組緩存
            int len = -1;                                                     //單次長度

            while((len = is.read(flush)) != -1)                                     //緩存獲得文件信息
                baos.write(flush,0,len);                                          //將緩存信息寫入操作對象

            baos.flush();                                                     //刷新操作對象
            return baos.toByteArray();                                           //將操作對象存儲的字節信息返回

        } catch (FileNotFoundException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();

        }finally {
            try {
                if(is != null)  is.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return null;
    }

    public static void BytetoFile(String filePath,byte[] src) {
        File dest = new File(filePath);                                              //建立文件對象

        InputStream is = null;                                           //輸入字節流
        OutputStream os = null;                                                    //輸出字節流

        try {
            is = new ByteArrayInputStream(src);                                        //將src寫入對象
            os = new FileOutputStream(dest);                                       //打開文件對象

            byte[] flush = new byte[5];                                              //字節輸出緩存
            int len = -1;                                                     //單次長度

            while((len = is.read(flush)) != -1)                                        //從將輸入流寫入緩存
                os.write(flush,0,len);                                           //將緩存寫進文件

            os.flush();                                                          //刷新輸出流

        } catch (FileNotFoundException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();

        }finally {
            try {
                if( os != null) os.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

改進版

package 學習資料.IO流;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 封裝拷貝
 * 封裝關閉
 *
 */
public class _5_1_FileUtiles_文件拷貝 {
    public static void main(String[] args) {

        //文件-->>文件
        try {
            //所有文件的複製都可以這樣使用,包括但不限於png,jpg,gif,txt,class等
            InputStream iStream = new FileInputStream("a.txt");
            OutputStream oStream = new FileOutputStream("a_copy.txt");

            BytetoFile(iStream,oStream);
            // close(iStream,oStream);

        } catch (IOException e) {
            e.printStackTrace();
        }

        //文件-->>字節數組
        byte[] information = null;
        try {

            InputStream iStream = new FileInputStream("a_copy.png");
            ByteArrayOutputStream oStream = new ByteArrayOutputStream();

            BytetoFile(iStream,oStream);
            information = oStream.toByteArray();
            // close(iStream,oStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //字節數組-->>文件
        try {
            //所有文件的複製都可以這樣使用,包括但不限於png,jpg,gif,txt,class等
            InputStream iStream = new ByteArrayInputStream(information);
            OutputStream oStream = new FileOutputStream("copy.png");

            BytetoFile(iStream,oStream);
            // close(iStream,oStream);

        } catch (IOException e) {
            e.printStackTrace();
        }
        /**
         * 之前想過明明可以直接將文件複製爲什麼還需要轉換成字節數組這個中間變量呢?
         * 轉換成字節數組的優點在於不只可以在本地進行傳輸(複製)文件了,因爲網絡只能接收字節流,所以可以將文件轉化成字節數組傳輸到其他主機上
         * 其次,字節數組可以進行加密,可以保證數據在網絡傳輸上的安全性
         * 還有就是可以將單個文件拆分成多個小文件,比如別人想爬取一個視頻但是隻能爬取到一個個小視頻無法整合,整合手段對方爬取不到從而達到保護文件的目的
         */
    }

    public static void BytetoFile(InputStream is,OutputStream os) {
        try(InputStream is1 =
                    new BufferedInputStream(is);            //JDK1.8中的try-with-resources語法糖,這樣寫就可以省掉close部分
            OutputStream os1 =
                    new BufferedOutputStream(os)) {          //可以省去close 的封裝操作1

            //Buffered可以提高效率操作方式沒變(類似與裝飾模式)
            byte[] flush = new byte[1024];                                        //字節輸出緩存
            int len = -1;                                                     //單次長度

            while((len = is1.read(flush)) != -1)                                       //從將輸入流寫入緩存
                os1.write(flush,0,len);                                              //將緩存寫進文件

            os1.flush();                                                         //刷新輸出流

        } catch (FileNotFoundException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();

        }
    }
    public static void close(InputStream is,OutputStream os) {
        try {
            if( os != null) os.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            if(is !=null)is.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //接口是一個特殊的類,也可以作爲參數
    //				↓可變參數
    public static void close(Closeable...ios) {
        for(Closeable io:ios) {
            try {
                if( io != null) io.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章