zoukankan      html  css  js  c++  java
  • 几种读取文件的方式和耗时(inputStream,BufferedInputStream,FileChannel, directMemory, pipe)

    public class IOTestMain {
    public static void main (String s[]){
    IOTestMain ioTestMain = new IOTestMain();
    ioTestMain.readFileTest();
    }

    private void closeOutStream (OutputStream stream){
    if (stream != null){
    try{
    stream.close();
    }catch (IOException e){
    System.out.println(e.getMessage());
    }

    }
    }
    private void closeInStream (InputStream inputStream){
    if (inputStream != null){
    try{
    inputStream.close();
    }catch (IOException e){
    System.out.println(e.getMessage());
    }

    }
    }
    //异步任务
    private void runTask(Pipe pipe,long fileLength,File in) {
    try(ZipOutputStream zos = new ZipOutputStream(Channels.newOutputStream(pipe.sink()));
    WritableByteChannel out = Channels.newChannel(zos)) {
    for (int i = 0; i < 10; i++) {
    //System.out.println("Begin"+i);
    zos.putNextEntry(new ZipEntry("test"+i+".txt"));
    FileChannel jpgChannel = new FileInputStream(in).getChannel();
    jpgChannel.transferTo(0, fileLength, out);
    jpgChannel.close();
    }
    zos.closeEntry();
    //zos.finish();
    }catch (Exception e){
    e.printStackTrace();
    }
    }

    public void readFileTest(){
    long time = System.currentTimeMillis();
    File zipFile = new File("C:\projects\dbdatafile\test.rar");
    File in = new File("C:\projects\dbdatafile\test.txt");
    ZipOutputStream zipOutputStream = null;
    FileOutputStream fileOutputStream = null;
    InputStream inputStream = null;
    ///use buffer
    BufferedInputStream bufferedInputStream = null;
    BufferedOutputStream bufferedOutputStream = null;

    //use file channel
    FileChannel fileChannel = null;
    WritableByteChannel writableByteChannel = null;

    MappedByteBuffer mappedByteBuffer = null;
    try{
    fileOutputStream = new FileOutputStream(zipFile);

    //第五种 异步方式耗时1.5+ S
    writableByteChannel = Channels.newChannel(fileOutputStream);
    Pipe pipe = Pipe.open();
    CompletableFuture.runAsync(() ->runTask(pipe,in.length(),in));
    ReadableByteChannel readableByteChannel = pipe.source();
    ByteBuffer buffer = ByteBuffer.allocate(((int) in.length()*10));
    while (readableByteChannel.read(buffer)>= 0) {
    buffer.flip();
    writableByteChannel.write(buffer);
    buffer.clear();
    }
    if (writableByteChannel != null && writableByteChannel.isOpen()){
    writableByteChannel.close();//如果不close,后面又直接重新new,会导致数据写不到磁盘
    }
    for ( int i = 0;i<10;i++){
    if (zipOutputStream == null){
    zipOutputStream = new ZipOutputStream(fileOutputStream);
    }
    inputStream = new FileInputStream(in);
    bufferedInputStream = new BufferedInputStream(inputStream);
    zipOutputStream.putNextEntry(new ZipEntry("test"+i+".txt"));
    bufferedOutputStream = new BufferedOutputStream(zipOutputStream);
    fileChannel = ((FileInputStream) inputStream).getChannel();
    writableByteChannel = Channels.newChannel(zipOutputStream);
    int temp = 0;
    //第一种方式耗时间20+秒
    while ((temp = inputStream.read())!= -1){
    zipOutputStream.write(temp);
    }
    //第二种方式耗时间0.8+秒
    while ((temp = bufferedInputStream.read())!= -1){
    bufferedOutputStream.write(temp);
    }
    //第三种方式耗时间0.4+秒
    fileChannel.transferTo(0,in.length(),writableByteChannel);
    //第四种,使用java堆外内存做缓冲区,耗时0.4+秒
    mappedByteBuffer =
    new RandomAccessFile(in,"r").getChannel().map(FileChannel.MapMode.READ_ONLY,0,in.length());
    writableByteChannel.write(mappedByteBuffer);
    closeInStream(inputStream);
    inputStream = null;
    }
    closeOutStream(zipOutputStream);
    zipOutputStream = null;
    }catch (Exception e){
    System.out.println("1:"+e.getStackTrace()+e.getMessage());
    }finally {
    closeOutStream(fileOutputStream);
    closeOutStream(zipOutputStream);
    closeOutStream(bufferedOutputStream);
    try{
    if (writableByteChannel != null && writableByteChannel.isOpen()){
    writableByteChannel.close();
    }
    }catch (Exception e){
    System.out.println("3.2:"+e.getStackTrace());
    }
    closeInStream(inputStream);
    closeInStream(bufferedInputStream);
    try{
    if (fileChannel != null && fileChannel.isOpen()){
    fileChannel.close();
    }
    }catch (Exception e){
    System.out.println("6:"+e.getStackTrace());
    }

    }
    printTimeCost(time);
    }
    private void printTimeCost(long timestart){
    long timeNow = System.currentTimeMillis();
    System.out.println("cost Time is : " + (timeNow -timestart));
    }

    }

  • 相关阅读:
    http://home.cnblogs.com/
    关于JS中的JSON
    JQuery中阻止事件冒泡方式及其区别
    jQuery判断元素是否是隐藏的代码
    jquery 绑定动态元素
    Js数组的操作push,pop,shift,unshift等方法详细介绍
    Jquery获取checkbox属性checked为undefined
    javascript时间戳和日期字符串相互转换
    关于jQuery新的事件绑定机制on()的使用技巧
    jQuery.extend()、jQuery.fn.extend()扩展方法示例详解
  • 原文地址:https://www.cnblogs.com/thinkqin/p/11957720.html
Copyright © 2011-2022 走看看