Java中使用内存映射实现大文件上传实例

 更新时间:2015年01月14日 11:53:17   投稿:junjie  
这篇文章主要介绍了Java中使用内存映射实现大文件上传实例,本文对比测试了FileInputStream 或者FileOutputStream 抑或RandomAccessFile的频繁读写操作,最后总结出映射到内存后进行读写以提高速度,需要的朋友可以参考下

在处理大文件时,如果利用普通的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 fis=new FileInputStream("/home/tobacco/test/res.txt"); 
            int sum=0; 
            int n; 
            long t1=System.currentTimeMillis(); 
            try { 
                while((n=fis.read())>=0){ 
                    sum+=n; 
                } 
            } catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 
            long t=System.currentTimeMillis()-t1; 
            System.out.println("sum:"+sum+"  time:"+t); 
        } catch (FileNotFoundException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 

        try { 
            FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); 
            BufferedInputStream bis=new BufferedInputStream(fis); 
            int sum=0; 
            int n; 
            long t1=System.currentTimeMillis(); 
            try { 
                while((n=bis.read())>=0){ 
                    sum+=n; 
                } 
            } catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 
            long t=System.currentTimeMillis()-t1; 
            System.out.println("sum:"+sum+"  time:"+t); 
        } catch (FileNotFoundException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 

        MappedByteBuffer buffer=null; 
        try { 
            buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244); 
            int sum=0; 
            int n; 
            long t1=System.currentTimeMillis(); 
            for(int i=0;i<1253244;i++){ 
                n=0x000000ff&buffer.get(i); 
                sum+=n; 
            } 
            long t=System.currentTimeMillis()-t1; 
            System.out.println("sum:"+sum+"  time:"+t); 
        } catch (FileNotFoundException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } catch (IOException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 

    } 

}


测试文件为一个大小为1253244字节的文件。测试结果:
复制代码 代码如下:

sum:220152087 time:1464 
sum:220152087 time:72 
sum: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 fis=new FileInputStream("/home/tobacco/test/res.txt"); 
            int sum=0; 
            int n; 
            long t1=System.currentTimeMillis(); 
            try { 
                while((n=fis.read())>=0){ 
                    //sum+=n; 
                } 
            } catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 
            long t=System.currentTimeMillis()-t1; 
            System.out.println("sum:"+sum+"  time:"+t); 
        } catch (FileNotFoundException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 

        try { 
            FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); 
            BufferedInputStream bis=new BufferedInputStream(fis); 
            int sum=0; 
            int n; 
            long t1=System.currentTimeMillis(); 
            try { 
                while((n=bis.read())>=0){ 
                    //sum+=n; 
                } 
            } catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 
            long t=System.currentTimeMillis()-t1; 
            System.out.println("sum:"+sum+"  time:"+t); 
        } catch (FileNotFoundException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 

        MappedByteBuffer buffer=null; 
        try { 
            buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244); 
            int sum=0; 
            int n; 
            long t1=System.currentTimeMillis(); 
            for(int i=0;i<1253244;i++){ 
                //n=0x000000ff&buffer.get(i); 
                //sum+=n; 
            } 
            long t=System.currentTimeMillis()-t1; 
            System.out.println("sum:"+sum+"  time:"+t); 
        } catch (FileNotFoundException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } catch (IOException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 

    } 

}


测试结果:
复制代码 代码如下:

sum:0 time:1458 
sum:0 time:67 
sum:0 time:8

由此可见,将文件部分或者全部映射到内存后进行读写,速度将提高很多。

这是因为内存映射文件首先将外存上的文件映射到内存中的一块连续区域,被当成一个字节数组进行处理,读写操作直接对内存进行操作,而后再将内存区域重新映射到外存文件,这就节省了中间频繁的对外存进行读写的时间,大大降低了读写时间。

相关文章

  • 解决JavaWeb-file.isDirectory()遇到的坑问题

    解决JavaWeb-file.isDirectory()遇到的坑问题

    JavaWeb开发中,使用`file.isDirectory()`判断路径是否为文件夹时,需要特别注意:该方法只能判断已存在的文件夹,若路径不存在,无论其实际是否应为文件夹,均会返回`false`,为了解决这个问题,可以采用正则表达式进行判断,但要求路径字符串的结尾必须添加反斜杠(\)
    2025-02-02
  • Java解决前端数据处理及乱码问题

    Java解决前端数据处理及乱码问题

    大伙们有没有遇到数据乱码的问题,真的是让人心情烦躁,今天就来教下大家数据怎么传输到前端以及乱码问题怎么解决的,需要的朋友可以参考一下
    2021-12-12
  • SpringBoot 单元测试JUnit的使用详解

    SpringBoot 单元测试JUnit的使用详解

    这篇文章主要介绍了SpringBoot 单元测试JUnit的使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • 解决mybatis-plus自定义xml的坑

    解决mybatis-plus自定义xml的坑

    这篇文章主要介绍了解决mybatis-plus自定义xml的坑,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • JAVA通过HttpURLConnection 上传和下载文件的方法

    JAVA通过HttpURLConnection 上传和下载文件的方法

    这篇文章主要介绍了JAVA通过HttpURLConnection 上传和下载文件的方法,非常具有实用价值,需要的朋友可以参考下
    2017-09-09
  • Java中List与数组相互转换实例分析

    Java中List与数组相互转换实例分析

    这篇文章主要介绍了Java中List与数组相互转换的方法,实例分析了Java中List与数组相互转换中容易出现的问题与相关的解决方法,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-05-05
  • 深入解析Jdk8中Stream流的使用让你脱离for循环

    深入解析Jdk8中Stream流的使用让你脱离for循环

    这篇文章主要介绍了Jdk8中Stream流的使用,让你脱离for循环,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-02-02
  • Java中notify()和notifyAll()的使用区别

    Java中notify()和notifyAll()的使用区别

    本文主要介绍了Java中notify()和notifyAll()的使用区别,文中通过示例代码介绍的非常详细,感兴趣的小伙伴们可以参考一下
    2021-06-06
  • springboot项目中实现访问druid内置监控页面

    springboot项目中实现访问druid内置监控页面

    这篇文章主要介绍了springboot项目中实现访问druid内置监控页面的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • Java简单实现猜数字游戏附C语言版本

    Java简单实现猜数字游戏附C语言版本

    猜数字是兴起于英国的益智类小游戏,起源于20世纪中期,一般由两个人或多人玩,也可以由一个人和电脑玩。游戏规则为一方出数字,一方猜,今天我们来用Java和C语言分别把这个小游戏写出来练练手
    2021-11-11

最新评论