java

xiaoxiao2022-06-12  21

解释:将文件的一段区域映射到内存中,比传统的文件处理速度要快很多

 

参考:

无格式输入流 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(); } }

 

相关资源:creo3.0-config.pro配置文件详解
转载请注明原文地址: https://www.6miu.com/read-4934155.html

最新回复(0)