码迷,mamicode.com
首页 > 其他好文 > 详细

文件拷贝

时间:2017-07-22 23:42:03      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:rom   osi   into   throw   bsp   system   文件拷贝   redo   buffer   

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;

public class FileCopy {
    public static void fileCopyByByte(String inPah, String outPah)
            throws FileNotFoundException, IOException {
        byte[] byteArray = new byte[1024];
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
                inPah));
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(outPah));
        int readCount = 0;
        while ((readCount = bis.read(byteArray)) != -1) {
            bos.write(byteArray, 0, readCount);
            bos.flush();
        }
        bis.close();
        bos.close();
    }

    public static void fileCopyByChar(String inPah, String outPah)
            throws FileNotFoundException, IOException {
        char[] charArray = new char[1024];
        BufferedReader reader = new BufferedReader(new FileReader(inPah));
        BufferedWriter writer = new BufferedWriter(new FileWriter(outPah));
        int readCount = 0;
        while ((readCount = reader.read(charArray)) != -1) {
            writer.write(charArray, 0, readCount);
            writer.flush();
        }
        reader.close();
        writer.close();
    }

    public static void fileCopyByFileChannel(String inPah,String outPah) throws FileNotFoundException,IOException{
        FileInputStream fis = new FileInputStream(inPah);
        FileOutputStream fos = new FileOutputStream(outPah);
        FileChannel fileChannel_from = fis.getChannel();
        FileChannel fileChannel_to = fos.getChannel();       

        ByteBuffer bytebuffer = ByteBuffer.allocate(1024);
        
        // Read data from file into ByteBuffer
        int bytesCount;
        while ((bytesCount = fileChannel_from.read(bytebuffer)) > 0) {
         //flip the buffer which set the limit to current position, and position to 0             
         bytebuffer.flip();
         //write data from ByteBuffer to file
         fileChannel_to.write(bytebuffer);
         //for the next read
         bytebuffer.clear();
        }
    }

    public static void main(String[] args) {
        try {
            long begin = System.currentTimeMillis();
            fileCopyByByte("e:/1.mkv", "e:/2.mkv");
            System.out.println(System.currentTimeMillis() - begin);
            begin = System.currentTimeMillis();
            fileCopyByFileChannel("e:/1.mkv", "e:/3.mkv");
            System.out.println(System.currentTimeMillis() - begin);
            begin = System.currentTimeMillis();
            Files.copy(new File("e:/1.mkv").toPath(), new File("e:/4.mkv").toPath());
            System.out.println(System.currentTimeMillis() - begin);
            
        } catch (FileNotFoundException e) {
                e.printStackTrace();
        } catch (IOException e) {
                e.printStackTrace();
        }
    }
}

 

文件拷贝

标签:rom   osi   into   throw   bsp   system   文件拷贝   redo   buffer   

原文地址:http://www.cnblogs.com/tonggc1668/p/7222983.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!