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.