html5国内网站,网站首页制作的过程,做房地产网站建设,购物网站php源代码在处理大文件时#xff0c;如果利用普通的FileInputStream 或者FileOutputStream 抑或RandomAccessFile 来进行频繁的读写操作#xff0c;都将导致进程因频繁读写外存而降低速度.如下为一个对比实验。package test;import java.io.BufferedInputStream;import java.io.FileIn…在处理大文件时如果利用普通的FileInputStream 或者FileOutputStream 抑或RandomAccessFile 来进行频繁的读写操作都将导致进程因频繁读写外存而降低速度.如下为一个对比实验。package test;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.RandomAccessFile;import java.nio.MappedByteBuffer;import java.nio.channels.FileChannel;public class Test {public static void main(String[] args) {try {FileInputStream fisnew FileInputStream(/home/tobacco/test/res.txt);int sum0;int n;long t1System.currentTimeMillis();try {while((nfis.read())0){sumn;}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}long tSystem.currentTimeMillis()-t1;System.out.println(sum:sum time:t);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {FileInputStream fisnew FileInputStream(/home/tobacco/test/res.txt);BufferedInputStream bisnew BufferedInputStream(fis);int sum0;int n;long t1System.currentTimeMillis();try {while((nbis.read())0){sumn;}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}long tSystem.currentTimeMillis()-t1;System.out.println(sum:sum time:t);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}MappedByteBuffer buffernull;try {buffernew RandomAccessFile(/home/tobacco/test/res.txt,rw).getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244);int sum0;int n;long t1System.currentTimeMillis();for(int i0;i1253244;i){n0x000000ffbuffer.get(i);sumn;}long tSystem.currentTimeMillis()-t1;System.out.println(sum:sum time:t);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}测试文件为一个大小为1253244字节的文件。测试结果sum:220152087 time:1464sum:220152087 time:72sum:220152087 time:25说明读数据无误。删去其中的数据处理部分。package test;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.RandomAccessFile;import java.nio.MappedByteBuffer;import java.nio.channels.FileChannel;public class Test {public static void main(String[] args) {try {FileInputStream fisnew FileInputStream(/home/tobacco/test/res.txt);int sum0;int n;long t1System.currentTimeMillis();try {while((nfis.read())0){//sumn;}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}long tSystem.currentTimeMillis()-t1;System.out.println(sum:sum time:t);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {FileInputStream fisnew FileInputStream(/home/tobacco/test/res.txt);BufferedInputStream bisnew BufferedInputStream(fis);int sum0;int n;long t1System.currentTimeMillis();try {while((nbis.read())0){//sumn;}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}long tSystem.currentTimeMillis()-t1;System.out.println(sum:sum time:t);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}MappedByteBuffer buffernull;try {buffernew RandomAccessFile(/home/tobacco/test/res.txt,rw).getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244);int sum0;int n;long t1System.currentTimeMillis();for(int i0;i1253244;i){//n0x000000ffbuffer.get(i);//sumn;}long tSystem.currentTimeMillis()-t1;System.out.println(sum:sum time:t);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}测试结果sum:0 time:1458sum:0 time:67sum:0 time:8由此可见将文件部分或者全部映射到内存后进行读写速度将提高很多。这是因为内存映射文件首先将外存上的文件映射到内存中的一块连续区域被当成一个字节数组进行处理读写操作直接对内存进行操作而后再将内存区域重新映射到外存文件这就节省了中间频繁的对外存进行读写的时间大大降低了读写时间。