Java中使用內(nèi)存映射實現(xiàn)大文件上傳實例
在處理大文件時,如果利用普通的FileInputStream 或者FileOutputStream 抑或RandomAccessFile 來進行頻繁的讀寫操作,都將導(dǎo)致進程因頻繁讀寫外存而降低速度.如下為一個對比實驗。
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字節(jié)的文件。測試結(jié)果:
sum:220152087 time:1464
sum:220152087 time:72
sum:220152087 time:25
說明讀數(shù)據(jù)無誤。刪去其中的數(shù)據(jù)處理部分。
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();
}
}
}
測試結(jié)果:
sum:0 time:1458
sum:0 time:67
sum:0 time:8
由此可見,將文件部分或者全部映射到內(nèi)存后進行讀寫,速度將提高很多。
這是因為內(nèi)存映射文件首先將外存上的文件映射到內(nèi)存中的一塊連續(xù)區(qū)域,被當(dāng)成一個字節(jié)數(shù)組進行處理,讀寫操作直接對內(nèi)存進行操作,而后再將內(nèi)存區(qū)域重新映射到外存文件,這就節(jié)省了中間頻繁的對外存進行讀寫的時間,大大降低了讀寫時間。
相關(guān)文章
解決JavaWeb-file.isDirectory()遇到的坑問題
JavaWeb開發(fā)中,使用`file.isDirectory()`判斷路徑是否為文件夾時,需要特別注意:該方法只能判斷已存在的文件夾,若路徑不存在,無論其實際是否應(yīng)為文件夾,均會返回`false`,為了解決這個問題,可以采用正則表達式進行判斷,但要求路徑字符串的結(jié)尾必須添加反斜杠(\)2025-02-02
JAVA通過HttpURLConnection 上傳和下載文件的方法
這篇文章主要介紹了JAVA通過HttpURLConnection 上傳和下載文件的方法,非常具有實用價值,需要的朋友可以參考下2017-09-09
Java中List與數(shù)組相互轉(zhuǎn)換實例分析
這篇文章主要介紹了Java中List與數(shù)組相互轉(zhuǎn)換的方法,實例分析了Java中List與數(shù)組相互轉(zhuǎn)換中容易出現(xiàn)的問題與相關(guān)的解決方法,具有一定參考借鑒價值,需要的朋友可以參考下2015-05-05
深入解析Jdk8中Stream流的使用讓你脫離for循環(huán)
這篇文章主要介紹了Jdk8中Stream流的使用,讓你脫離for循環(huán),本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02
Java中notify()和notifyAll()的使用區(qū)別
本文主要介紹了Java中notify()和notifyAll()的使用區(qū)別,文中通過示例代碼介紹的非常詳細,感興趣的小伙伴們可以參考一下2021-06-06
springboot項目中實現(xiàn)訪問druid內(nèi)置監(jiān)控頁面
這篇文章主要介紹了springboot項目中實現(xiàn)訪問druid內(nèi)置監(jiān)控頁面的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
Java簡單實現(xiàn)猜數(shù)字游戲附C語言版本
猜數(shù)字是興起于英國的益智類小游戲,起源于20世紀中期,一般由兩個人或多人玩,也可以由一個人和電腦玩。游戲規(guī)則為一方出數(shù)字,一方猜,今天我們來用Java和C語言分別把這個小游戲?qū)懗鰜砭毦毷?/div> 2021-11-11最新評論

