通过字节流、缓冲字节流、随机文件访问、文件内存映射 计算 Git 安装包的 crc32校验和

* 通过字节流、缓冲字节流、随机文件访问、文件内存映射 计算文件 crc32校验和 * author: sunny * date: 2022/7/12 15:53 public class CalReadFileTime { private static String filePath = "D:\\下载\\chrome\\Git-2.37.0-64-bit.exe" ; public static void main ( String [ ] args ) { inputStream ( ) ; bufferedInputStream ( ) ; randomAccessFile ( ) ; fileMemoryMap ( ) ; * 普通字节流,一个字节一个字节从硬盘读取数据 private static void inputStream ( ) { CRC32 crc32 = new CRC32 ( ) ; Instant start = Instant . now ( ) ; try ( InputStream inputStream = new FileInputStream ( filePath ) ) { int abyteValue ; while ( ( abyteValue = inputStream . read ( ) ) != - 1 ) { crc32 . update ( abyteValue ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; Duration duration = Duration . between ( start , Instant . now ( ) ) ; System . out . println ( "inputStream:" + crc32 . getValue ( ) + " duration:" + duration . toMillis ( ) ) ; * 带缓冲的字节流,没有数据时会从硬盘读取一块数据到缓冲区中 private static void bufferedInputStream ( ) { CRC32 crc32 = new CRC32 ( ) ; Instant start = Instant . now ( ) ; try ( BufferedInputStream bufferedInputStream = new BufferedInputStream ( new FileInputStream ( filePath ) ) ) { int abyteValue ; while ( ( abyteValue = bufferedInputStream . read ( ) ) != - 1 ) { crc32 . update ( abyteValue ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; Duration duration = Duration . between ( start , Instant . now ( ) ) ; System . out . println ( "bufferedInputStream:" + crc32 . getValue ( ) + "duration:" + duration . toMillis ( ) ) ; * 随机文件访问,也是一个字节一个字节读。因为提供了访问指针,所以理论上随度比 inputStream 慢 private static void randomAccessFile ( ) { CRC32 crc32 = new CRC32 ( ) ; Instant start = Instant . now ( ) ; try ( RandomAccessFile randomAccessFile = new RandomAccessFile ( filePath , "r" ) ) { int abyteValue ; while ( ( abyteValue = randomAccessFile . read ( ) ) != - 1 ) { crc32 . update ( abyteValue ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; Duration duration = Duration . between ( start , Instant . now ( ) ) ; System . out . println ( "randomAccessFile:" + crc32 . getValue ( ) + "duration:" + duration . toMillis ( ) ) ; * 文件内存映射,通过虚拟内存将整个或者部分文件内容加载到内存中 private static void fileMemoryMap ( ) { CRC32 crc32 = new CRC32 ( ) ; Instant start = Instant . now ( ) ; try ( FileChannel fileChannel = FileChannel . open ( Paths . get ( filePath ) ) ) { ByteBuffer byteBuffer = fileChannel . map ( FileChannel . MapMode . READ_ONLY , 0 , fileChannel . size ( ) ) ; while ( byteBuffer . hasRemaining ( ) ) { crc32 . update ( byteBuffer . get ( ) ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; Duration duration = Duration . between ( start , Instant . now ( ) ) ; System . out . println ( "fileMemoryMap:" + crc32 . getValue ( ) + "duration:" + duration . toMillis ( ) ) ;
inputStream:3199121536 duration:43084
bufferedInputStream:3199121536 duration:148
randomAccessFile:3199121536 duration:38037
fileMemoryMap:3199121536 duration:147

发现带缓存的输入流和文件内存映射这两种方式是最快的。

一、FileInputStream(文件输入流) FileInputStream是InputStream的子类被称为文件输入流,是从文件流中读取数据。每次都从硬盘中读取读取速度缓慢。 完整读取FileInputStream的所有字节共有俩方式:(1)逐个字节读取;(2)批量读取。 代码如下: import java.io.FileInputStream; import jav
1.按照字节读取 public String readFileByByte(String filePath){ long beginTime =System.currentTimeMillis(); StringBuffer stringBuffer=new StringBuffer(); byte[] buffer=new byte[2048];//现在一次读取2048字节,可以一次性读取文件的...
上一篇我们介绍了使用java写入到文件,不同io类速度差异较大,本篇我们将介绍从文件读取数据,速度的差异。 测试文件text01.txt,text02.txt,text03.txt, 文件大小均为85938KB, 测试代码:package com.win.sample;import java.io.BufferedInputStream; import java.io.BufferedOutput
1. 使用 `FileInputStream` 类以字节的方式读取文件。 2. 使用 `BufferedReader` 在字符输入流上包装一个缓冲区,以行为单位读取文件。 3. 使用 `Scanner` 类以分隔符为标志读取文件。 4. 使用 `Files` 类的 `readAllLines` 方法一次性读取所有行。 5. 使用 `ObjectInputStream` 反序列化对象从文件读取对象。 6. 使用 `FileChannel` 从文件读取内容。 例如,下面是使用 `BufferedReader` 读取文件的例子: BufferedReader reader = new BufferedReader(new FileReader("filename.txt")); try { String line = null; while ((line = reader.readLine()) != null) { // process the line } finally { reader.close(); MySQLNonTransientConnectionException: Could not create connection to database server 解决办法 15253
MySQLNonTransientConnectionException: Could not create connection to database server 解决办法 m0_65142122: 哈哈哈大哥,解决问题了 MySQLNonTransientConnectionException: Could not create connection to database server 解决办法 鱼鱼鱼BOL: 你真是我爹啊表情包解决了 android开发设置圆形、圆角图片(全网最简单,不会你打我) 好像你说的有道理 android开发设置圆形、圆角图片(全网最简单,不会你打我) v1119034417: 关键是我的背景颜色不是白的啊,这样做出来的效果不一样