日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java 提交的内存_Java使用内存映射实现大文件的上传

發布時間:2023/12/2 java 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 提交的内存_Java使用内存映射实现大文件的上传 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在處理大文件時,如果利用普通的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

由此可見,將文件部分或者全部映射到內存后進行讀寫,速度將提高很多。

這是因為內存映射文件首先將外存上的文件映射到內存中的一塊連續區域,被當成一個字節數組進行處理,讀寫操作直接對內存進行操作,而后再將內存區域重新映射到外存文件,這就節省了中間頻繁的對外存進行讀寫的時間,大大降低了讀寫時間。

總結

以上是生活随笔為你收集整理的java 提交的内存_Java使用内存映射实现大文件的上传的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。