• 文件拷贝io nio比较


    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.MappedByteBuffer;
    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();
            }
            fileChannel_from.close();
            fileChannel_to.close();
        }
        public static void fileCopyByFileChannelMap(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();       
            
            MappedByteBuffer bytebuffer = fileChannel_from.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel_from.size());
            fileChannel_to.write(bytebuffer);
            bytebuffer.clear();
            fileChannel_from.close();
            fileChannel_to.close();
        }
    
        public static void main(String[] args) {
            try {
                String in = "E:/小说/左道旁门.txt";
                long begin = System.currentTimeMillis();
                fileCopyByByte(in, "e:/2");
                System.out.println(System.currentTimeMillis() - begin);
                begin = System.currentTimeMillis();
                fileCopyByFileChannel(in, "e:/3");
                System.out.println(System.currentTimeMillis() - begin);
                begin = System.currentTimeMillis();
                fileCopyByFileChannelMap(in, "e:/4");
                System.out.println(System.currentTimeMillis() - begin);
                begin = System.currentTimeMillis();
                Files.copy(new File(in).toPath(), new File("e:/5").toPath());
                System.out.println(System.currentTimeMillis() - begin);
                
            } catch (FileNotFoundException e) {
                    e.printStackTrace();
            } catch (IOException e) {
                    e.printStackTrace();
            }
        }
    }

    十几M小文件

    360
    843
    672
    641

    100多M文件

    19547
    5610
    2703
    8718

    300多M文件

    41156
    13609
    8563
    9500

    1.7G文件

    202156
    225109

    出错,可能超过限制
    163719

  • 相关阅读:
    8-4:Mysql数据库编程基础知识
    adb——Android的ADB工具使用
    BroadcastReceiver--Android广播机制
    怎样投篮更准
    《算法七》(深度寻路算法)
    《算法六》(有序二叉树)
    《算法五》(N叉树定义+增删改查)
    《算法四》(二分排序+汉诺塔问题)
    《算法三》(归并排序)
    《算法二》(希尔排序+基数排序+桶排序)
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/7222983.html
Copyright © 2020-2023  润新知