速度从最快到最慢的排序是:
1,内存映射文件
2,带缓冲的输入流
3,普通输入流
4,随机访问文件
例子:
package javaPratice;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.*;
import java.util.zip.CRC32;
import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
public class MemoryMapUtil {
/**
* @category 普通输入流的执行
* @param fileName
* @return
* @throws IOException
*/
public static long totalInputStream(Path fileName) throws IOException{
try(InputStream in =Files.newInputStream(fileName)){
CRC32 crc=new CRC32();
int c;
while((c=in.read())!=-1){
crc.update(c);
}
return crc.getValue();
}
}
/**
* @category 带有缓冲的输入流的执行
* @param filename
* @return
* @throws IOException
*/
public static long totalBufferedInputStream(Path filename) throws IOException{
try(InputStream in =new BufferedInputStream(Files.newInputStream(filename))){
CRC32 crc=new CRC32();
int c;
while((c=in.read())!=-1){
crc.update(c);
}
return crc.getValue();
}
}
/**
* @category 随机访问文件执行
* @param filename
* @return
* @throws IOException
*/
public static long totalRandomAccessFile(Path filename) throws IOException{
try(RandomAccessFile file =new RandomAccessFile(filename.toFile(),"r")){
long length = file.length();
CRC32 crc=new CRC32();
for(long p=0;p<length;p++){
file.seek(p);
int c=file.readByte();
crc.update(c);
}
return crc.getValue();
}
}
/**
* @category 内存映射文件访问执行
* @param filename
* @return
* @throws IOException
*/
public static long totalMappedFIle(Path filename) throws IOException{
try(FileChannel channel = FileChannel.open(filename)){
CRC32 crc=new CRC32();
int length=(int)channel.size();
MappedByteBuffer buffer=channel.map(FileChannel.MapMode.READ_ONLY, 0, length);
for(int p=0;p<length;p++){
int c=buffer.get(p);
crc.update(c);
}
return crc.getValue();
}
}
public static void main(String args[]) throws IOException{
Path filename=Paths.get("C:/Users/Administrator.FZQ3RSFK73H2UEE/Desktop/xqb.txt");
long start =System.currentTimeMillis();
long crcValue=totalInputStream(filename);
long end =System.currentTimeMillis();
System.out.println("Input Stream");
System.out.println(Long.toHexString(crcValue));
System.out.println((end - start));
start =System.currentTimeMillis();
crcValue=totalBufferedInputStream(filename);
end =System.currentTimeMillis();
System.out.println("buffered Input Stream");
System.out.println(Long.toHexString(crcValue));
System.out.println((end - start));
start =System.currentTimeMillis();
crcValue=totalRandomAccessFile(filename);
end =System.currentTimeMillis();
System.out.println("total Random AccessFile");
System.out.println(Long.toHexString(crcValue));
System.out.println((end - start));
start =System.currentTimeMillis();
crcValue=totalMappedFIle(filename);
end =System.currentTimeMillis();
System.out.println("total MappedFile");
System.out.println(Long.toHexString(crcValue));
System.out.println((end - start));
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.*;
import java.util.zip.CRC32;
import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
public class MemoryMapUtil {
/**
* @category 普通输入流的执行
* @param fileName
* @return
* @throws IOException
*/
public static long totalInputStream(Path fileName) throws IOException{
try(InputStream in =Files.newInputStream(fileName)){
CRC32 crc=new CRC32();
int c;
while((c=in.read())!=-1){
crc.update(c);
}
return crc.getValue();
}
}
/**
* @category 带有缓冲的输入流的执行
* @param filename
* @return
* @throws IOException
*/
public static long totalBufferedInputStream(Path filename) throws IOException{
try(InputStream in =new BufferedInputStream(Files.newInputStream(filename))){
CRC32 crc=new CRC32();
int c;
while((c=in.read())!=-1){
crc.update(c);
}
return crc.getValue();
}
}
/**
* @category 随机访问文件执行
* @param filename
* @return
* @throws IOException
*/
public static long totalRandomAccessFile(Path filename) throws IOException{
try(RandomAccessFile file =new RandomAccessFile(filename.toFile(),"r")){
long length = file.length();
CRC32 crc=new CRC32();
for(long p=0;p<length;p++){
file.seek(p);
int c=file.readByte();
crc.update(c);
}
return crc.getValue();
}
}
/**
* @category 内存映射文件访问执行
* @param filename
* @return
* @throws IOException
*/
public static long totalMappedFIle(Path filename) throws IOException{
try(FileChannel channel = FileChannel.open(filename)){
CRC32 crc=new CRC32();
int length=(int)channel.size();
MappedByteBuffer buffer=channel.map(FileChannel.MapMode.READ_ONLY, 0, length);
for(int p=0;p<length;p++){
int c=buffer.get(p);
crc.update(c);
}
return crc.getValue();
}
}
public static void main(String args[]) throws IOException{
Path filename=Paths.get("C:/Users/Administrator.FZQ3RSFK73H2UEE/Desktop/xqb.txt");
long start =System.currentTimeMillis();
long crcValue=totalInputStream(filename);
long end =System.currentTimeMillis();
System.out.println("Input Stream");
System.out.println(Long.toHexString(crcValue));
System.out.println((end - start));
start =System.currentTimeMillis();
crcValue=totalBufferedInputStream(filename);
end =System.currentTimeMillis();
System.out.println("buffered Input Stream");
System.out.println(Long.toHexString(crcValue));
System.out.println((end - start));
start =System.currentTimeMillis();
crcValue=totalRandomAccessFile(filename);
end =System.currentTimeMillis();
System.out.println("total Random AccessFile");
System.out.println(Long.toHexString(crcValue));
System.out.println((end - start));
start =System.currentTimeMillis();
crcValue=totalMappedFIle(filename);
end =System.currentTimeMillis();
System.out.println("total MappedFile");
System.out.println(Long.toHexString(crcValue));
System.out.println((end - start));
}
}