java的文件操作处理时间数据

速度从最快到最慢的排序是:
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));
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值