最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java使用BIO和NIO進行文件操作對比代碼示例

 更新時間:2020年05月13日 15:08:37   作者:玄同太子  
這篇文章主要介紹了Java使用BIO和NIO進行文件操作對比代碼示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

什么是Java NIO?

同步非阻塞io模式,拿燒開水來說,NIO的做法是叫一個線程不斷的輪詢每個水壺的狀態(tài),看看是否有水壺的狀態(tài)發(fā)生了改變,從而進行下一步的操作。
Java NIO有三大組成部分:Buffer,Channel,Selector,通過事件驅動模式實現了什么時候有數據可讀的問題。

什么是Java BIO?

同步阻塞IO模式,數據的讀取寫入必須阻塞在一個線程內等待其完成。這里使用那個經典的燒開水例子,這里假設一個燒開水的場景,有一排水壺在燒開水,BIO的工作模式就是, 叫一個線程停留在一個水壺那,直到這個水壺燒開,才去處理下一個水壺。但是實際上線程在等待水壺燒開的時間段什么都沒有做。不知道io操作中什么時候有數據可讀,所以一直是阻塞的模式。

1、讀文件

package com.zhi.test;

import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * 文件讀取,緩沖區(qū)大?。˙F_SIZE)對NIO的性能影響特別大,對BIO無影響<br>
 * 10M的文件,BIO耗時87毫秒,NIO耗時68毫秒,Files.read耗時62毫秒
 * 
 * @author 張遠志
 * @since 2020年5月9日19:20:49
 *
 */
public class FileRead {
  /**
   * 緩沖區(qū)大小
   */
  private static final int BF_SIZE = 1024;

  /**
   * 使用BIO讀取文件
   * 
   * @param fileName 待讀文件名
   * @return
   * @throws IOException
   */
  public static String bioRead(String fileName) throws IOException {
    long startTime = System.currentTimeMillis();
    try {
      FileReader reader = new FileReader(fileName);

      StringBuffer buf = new StringBuffer();
      char[] cbuf = new char[BF_SIZE];
      while (reader.read(cbuf) != -1) {
        buf.append(cbuf);
      }
      reader.close();
      return buf.toString();
    } finally {
      System.out.println("使用BIO讀取文件耗時:" + (System.currentTimeMillis() - startTime) + "毫秒");
    }
  }

  /**
   * 使用NIO讀取文件
   * 
   * @param fileName 待讀文件名
   * @return
   * @throws IOException
   */
  public static String nioRead1(String fileName) throws IOException {
    long startTime = System.currentTimeMillis();
    try {
      FileInputStream input = new FileInputStream(fileName);
      FileChannel channel = input.getChannel();

      CharsetDecoder decoder = Charset.defaultCharset().newDecoder();
      StringBuffer buf = new StringBuffer();
      CharBuffer cBuf = CharBuffer.allocate(BF_SIZE);
      ByteBuffer bBuf = ByteBuffer.allocate(BF_SIZE);
      while (channel.read(bBuf) != -1) {
        bBuf.flip();
        decoder.decode(bBuf, cBuf, false); // 解碼,byte轉char,最后一個參數非常關鍵
        bBuf.clear();
        buf.append(cBuf.array(), 0, cBuf.position());
        cBuf.compact(); // 壓縮
      }
      input.close();
      return buf.toString();
    } finally {
      System.out.println("使用NIO讀取文件耗時:" + (System.currentTimeMillis() - startTime) + "毫秒");
    }
  }

  /**
   * 使用Files.read讀取文件
   * 
   * @param fileName 待讀文件名
   * @return
   * @throws IOException
   */
  public static String nioRead2(String fileName) throws IOException {
    long startTime = System.currentTimeMillis();
    try {
      byte[] byt = Files.readAllBytes(Paths.get(fileName));
      return new String(byt);
    } finally {
      System.out.println("使用Files.read讀取文件耗時:" + (System.currentTimeMillis() - startTime) + "毫秒");
    }
  }

  public static void main(String[] args) throws IOException {
    String fileName = "E:/source.txt";
    FileRead.bioRead(fileName);
    FileRead.nioRead1(fileName);
    FileRead.nioRead2(fileName);
  }
}

2、寫文件

package com.zhi.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;

/**
 * 文件寫<br>
 * 10M的數據,BIO耗時45毫秒,NIO耗時42毫秒,Files.write耗時24毫秒
 * 
 * @author 張遠志
 * @since 2020年5月9日21:04:40
 *
 */
public class FileWrite {
  /**
   * 使用BIO進行文件寫
   * 
   * @param fileName 文件名稱
   * @param content 待寫內存
   * @throws IOException
   */
  public static void bioWrite(String fileName, String content) throws IOException {
    long startTime = System.currentTimeMillis();
    try {
      FileWriter writer = new FileWriter(fileName);
      writer.write(content);
      writer.close();
    } finally {
      System.out.println("使用BIO寫文件耗時:" + (System.currentTimeMillis() - startTime) + "毫秒");
    }
  }

  /**
   * 使用NIO進行文件寫
   * 
   * @param fileName 文件名稱
   * @param content 待寫內存
   * @throws IOException
   */
  public static void nioWrite1(String fileName, String content) throws IOException {
    long startTime = System.currentTimeMillis();
    try {
      FileOutputStream out = new FileOutputStream(fileName);
      FileChannel channel = out.getChannel();
      ByteBuffer buf = ByteBuffer.wrap(content.getBytes());
      channel.write(buf);
      out.close();
    } finally {
      System.out.println("使用NIO寫文件耗時:" + (System.currentTimeMillis() - startTime) + "毫秒");
    }
  }

  /**
   * 使用Files.write進行文件寫
   * 
   * @param fileName 文件名稱
   * @param content 待寫內存
   * @throws IOException
   */
  public static void nioWrite2(String fileName, String content) throws IOException {
    long startTime = System.currentTimeMillis();
    try {
      File file = new File(fileName);
      if (!file.exists()) {
        file.createNewFile();
      }
      Files.write(file.toPath(), content.getBytes(), StandardOpenOption.WRITE);
    } finally {
      System.out.println("使用Files.write寫文件耗時:" + (System.currentTimeMillis() - startTime) + "毫秒");
    }
  }

  public static void main(String[] args) throws IOException {
    String content = FileRead.nioRead2("E:/source.txt");
    String target1 = "E:/target1.txt", target2 = "E:/target2.txt", target3 = "E:/target3.txt";
    FileWrite.bioWrite(target1, content);
    FileWrite.nioWrite1(target2, content);
    FileWrite.nioWrite2(target3, content);
  }
}

3、復制文件

package com.zhi.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * 文件復制<br>
 * 10M的文件,bio耗時56毫秒,nio耗時12毫秒,Files.copy耗時10毫秒
 * 
 * @author 張遠志
 * @since 2020年5月9日17:18:01
 *
 */
public class FileCopy {
  /**
   * 使用BIO復制一個文件
   * 
   * @param target 源文件
   * @param source 目標文件
   * 
   * @throws IOException
   */
  public static void bioCopy(String source, String target) throws IOException {
    long startTime = System.currentTimeMillis();
    try {
      FileInputStream fin = new FileInputStream(source);
      FileOutputStream fout = new FileOutputStream(target);

      byte[] byt = new byte[1024];
      while (fin.read(byt) > -1) {
        fout.write(byt);
      }

      fin.close();
      fout.close();
    } finally {
      System.out.println("使用BIO復制文件耗時:" + (System.currentTimeMillis() - startTime) + "毫秒");
    }
  }

  /**
   * 使用NIO復制一個文件
   * 
   * @param target 源文件
   * @param source 目標文件
   * 
   * @throws IOException
   */
  public static void nioCopy1(String source, String target) throws IOException {
    long startTime = System.currentTimeMillis();
    try {
      FileInputStream fin = new FileInputStream(source);
      FileChannel inChannel = fin.getChannel();
      FileOutputStream fout = new FileOutputStream(target);
      FileChannel outChannel = fout.getChannel();

      inChannel.transferTo(0, inChannel.size(), outChannel);

      fin.close();
      fout.close();
    } finally {
      System.out.println("使用NIO復制文件耗時:" + (System.currentTimeMillis() - startTime) + "毫秒");
    }
  }

  /**
   * 使用Files.copy復制一個文件
   * 
   * @param target 源文件
   * @param source 目標文件
   * 
   * @throws IOException
   */
  public static void nioCopy2(String source, String target) throws IOException {
    long startTime = System.currentTimeMillis();
    try {
      File file = new File(target);
      if (file.exists()) {
        file.delete();
      }
      Files.copy(Paths.get(source), file.toPath());
    } finally {
      System.out.println("使用Files.copy復制文件耗時:" + (System.currentTimeMillis() - startTime) + "毫秒");
    }
  }

  public static void main(String[] args) throws IOException {
    String source = "E:/source.txt";
    String target1 = "E:/target1.txt", target2 = "E:/target2.txt", target3 = "E:/target3.txt";
    FileCopy.bioCopy(source, target1);
    FileCopy.nioCopy1(source, target2);
    FileCopy.nioCopy2(source, target3);
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Java中的分割字符串?split(“.”)無效問題

    Java中的分割字符串?split(“.”)無效問題

    這篇文章主要介紹了Java中的分割字符串?split(“.”)無效問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • 詳解Java實現數據結構之并查集

    詳解Java實現數據結構之并查集

    并查集這種數據結構,可能出現的頻率不是那么高,但是還會經常性的見到,其理解學習起來非常容易,通過本文,一定能夠輕輕松松搞定并查集
    2021-06-06
  • Java代碼中與Lua相互調用實現詳解

    Java代碼中與Lua相互調用實現詳解

    這篇文章主要為大家介紹了Java代碼中與Lua相互調用實現詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • SpringBoot?如何將項目打包成?jar?包

    SpringBoot?如何將項目打包成?jar?包

    這篇文章主要介紹了SpringBoot如何將項目打包成jar包,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08
  • java循環(huán)練習的簡單代碼實例

    java循環(huán)練習的簡單代碼實例

    本篇文章介紹了,java中循環(huán)練習的一些簡單代碼實例。需要的朋友參考下
    2013-04-04
  • 淺談Java內省機制

    淺談Java內省機制

    本文主要介紹了Java內省機制,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08
  • Spring Data + Thymeleaf 3 + Bootstrap 4 實現分頁器實例代碼

    Spring Data + Thymeleaf 3 + Bo

    本篇文章主要介紹了Spring Data + Thymeleaf 3 + Bootstrap 4 實現分頁器實例代碼,非常具有實用價值,需要的朋友可以參考下
    2017-05-05
  • 了解JAVA Future類

    了解JAVA Future類

    Future是并發(fā)編程中的一種設計模式,Future它代表一個異步計算的結果,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,下面小編和大家來一起學習一下吧
    2019-06-06
  • java定時器timer的使用方法代碼示例

    java定時器timer的使用方法代碼示例

    這篇文章主要介紹了java定時器timer的使用方法代碼示例,向大家分享了兩部分代碼,詳細內容請參見正文,還是比較不錯的,需要的朋友可以參考下。
    2017-11-11
  • mybatis插入數據不返回主鍵id的可能原因及解決方式

    mybatis插入數據不返回主鍵id的可能原因及解決方式

    這篇文章主要介紹了mybatis插入數據不返回主鍵id的可能原因及解決方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08

最新評論

麻城市| 九龙县| 房产| 龙游县| 吉木乃县| 安宁市| 绥德县| 拜城县| 太仓市| 湘西| 凤庆县| 巴青县| 祁连县| 平和县| 铜山县| 马山县| 漯河市| 公主岭市| 定州市| 都江堰市| 建昌县| 龙川县| 桑植县| 容城县| 呼和浩特市| 赤城县| 澜沧| 霍州市| 商城县| 梓潼县| 柳河县| 星子县| 甘孜县| 滁州市| 宁波市| 宁德市| 大石桥市| 房产| 桃园市| 加查县| 通化市|