java_croe 学习笔记之新IO---java.nio 之内存映射文件
2009-08-25 23:37
501 查看
解释:将文件的一段区域映射到内存中,比传统的文件处理速度要快很多
参考:
无格式输入流 110秒
缓冲输入流 9.9秒
随机存取文件 162秒
内存映射文件 7.2秒
例子
package twelve; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.util.zip.CRC32; /** @Title NIOTTest.java @description TODO @author qinpeng @date Aug 25, 2009 10:23:26 PM */ public class NIOTTest { public static void main(String[] args) { String fileName = "d:\\IOTest.pdf"; System.out.println("inputStream"); long start = System.currentTimeMillis(); long crcValue = checksumInputStreanm(fileName); long end = System.currentTimeMillis(); System.out.println(Long.toHexString(crcValue)); System.out.println((end - start)+"耗时"); System.out.println("BufferedinputStream"); start = System.currentTimeMillis(); crcValue = checksumInputStreanm(fileName); end = System.currentTimeMillis(); System.out.println(Long.toHexString(crcValue)); System.out.println((end - start)+"耗时"); System.out.println("RandomAccessFileinputStream"); start = System.currentTimeMillis(); crcValue = checksumInputStreanm(fileName); end = System.currentTimeMillis(); System.out.println(Long.toHexString(crcValue)); System.out.println((end - start)+"耗时"); System.out.println(" MappedFile inputStream"); start = System.currentTimeMillis(); crcValue = checksumInputStreanm(fileName); end = System.currentTimeMillis(); System.out.println(Long.toHexString(crcValue)); System.out.println((end - start)+"耗时"); } public static long checksumInputStreanm(String fileName){ CRC32 crc = new CRC32(); try { InputStream in = new FileInputStream(fileName); int c; while((c=in.read())!=-1){ crc.update(c); } } catch (FileNotFoundException e) { e.printStackTrace(); System.err.print("NIOTTest--checksumInputStreanm--new FileInputStream is not found"); } catch(IOException ioe){ ioe.printStackTrace(); System.err.print("NIOTTest--checksumInputStreanm--new FileInputStream'read append IOException"); } return crc.getValue(); } public static long checksumBufferedInputStream(String fileName){ CRC32 crc = new CRC32(); try { InputStream in = new BufferedInputStream(new FileInputStream(fileName)); int c; while((c=in.read())!=-1){ crc.update(c); } } catch (FileNotFoundException e) { e.printStackTrace(); System.err.print("NIOTTest--checksumBufferedInputStream--new FileInputStream is not found"); } catch(IOException ioe){ ioe.printStackTrace(); System.err.print("NIOTTest--checksumBufferedInputStream--new FileInputStream'read append IOException"); } return crc.getValue(); } public static long checksumRondomAccessFileInputStream(String fileName){ CRC32 crc = new CRC32(); try { RandomAccessFile file = new RandomAccessFile(fileName,"r"); int c; while((c=file.read())!=-1){ crc.update(c); } } catch (FileNotFoundException e) { e.printStackTrace(); System.err.print("NIOTTest--checksumRondomAccessFileInputStream--new FileInputStream is not found"); } catch(IOException ioe){ ioe.printStackTrace(); System.err.print("NIOTTest--checksumRondomAccessFileInputStream--new FileInputStream'read append IOException"); } return crc.getValue(); } public static long checksumMappedFile(String fileName){ CRC32 crc = new CRC32(); try { FileInputStream in = new FileInputStream(fileName); FileChannel channel = in.getChannel(); int length = (int) channel.size(); MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, length); for(int p = 0;p<length;p++){ int c = buffer.getInt(p); crc.update(c); } } catch (FileNotFoundException e) { e.printStackTrace(); System.err.print("NIOTTest--checksumRondomAccessFileInputStream--new FileInputStream is not found"); } catch(IOException ioe){ ioe.printStackTrace(); System.err.print("NIOTTest--checksumRondomAccessFileInputStream--new FileInputStream'read append IOException"); } return crc.getValue(); } }
阅读更多
相关文章推荐
- java_croe 学习笔记之新IO---java.nio 之内存映射文件
- [Java][IO]JAVA NIO之浅谈内存映射文件原理与DirectMemory
- [Java][IO]JAVA NIO之浅谈内存映射文件原理与DirectMemory
- Java NIO笔记之内存映射文件
- java nio 映射文件内容到内存
- java基础-用IO演示文件的复制过程(学习笔记)
- java io文件学习笔记
- Java中用内存映射NIO处理大文件
- Java学习笔记(五):文件IO
- 【JavaNIO的深入研究4】内存映射文件I/O,大文件读写操作,Java nio之MappedByteBuffer,高效文件/内存映射
- Java NIO 学习笔记(六)----异步文件通道 AsynchronousFileChannel
- JAVA NIO 内存映射文件原理与DirectMemory
- JAVA NIO之浅谈内存映射文件原理与DirectMemory
- 【Java学习笔记】18.Java 流(Stream)、文件(File)和IO
- java NIO非阻塞式IO网络编程学习笔记(一)
- Java-NIO(五):通道(Channel)的数据传输与内存映射文件
- JAVA NIO之浅谈内存映射文件原理与DirectMemory
- 黑马程序员_O‘Reilly java nio学习笔记之通道_内存映射文件&&Channel-to-Channel 传输
- Java IO学习笔记(五):内存操作流
- Java学习笔记——File类文件管理及IO读写、复制操作