Using memory mapping in Java to implement large file upload instance _java

Source: Internet
Author: User

When dealing with large files, if the use of ordinary fileinputstream or FileOutputStream or randomaccessfile to do frequent read and write operations, will cause the process of frequent reading and writing to reduce the speed of memory. The following is a contrastive experiment.

Copy Code code as follows:

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 ();
}

}

}


The test file is a file size of 1253244 bytes. Test results:
Copy Code code as follows:

sum:220152087 time:1464
sum:220152087 time:72
sum:220152087 time:25

Note that the read data is correct. Delete the data Processing section.
Copy Code code as follows:

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 ();
}

}

}


Test results:
Copy Code code as follows:

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

This shows that the file part or all of the map to memory after reading and writing, the speed will improve a lot.

This is because the memory-mapped file first maps the files on the external memory to a contiguous area in RAM. is treated as an array of bytes, read and write operations directly to the memory operation, and then the memory area to be mapped to the external storage file, which saves the intermediate frequently to save for reading and writing time, Greatly reduces the reading and writing time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.