教程集 www.jiaochengji.com
教程集 >  脚本编程  >  java  >  正文 Java读取文件性能优化程序代码

Java读取文件性能优化程序代码

发布时间:2016-10-24   编辑:jiaochengji.com
教程集为您提供Java读取文件性能优化程序代码等资源,欢迎您收藏本站,我们将为您提供最新的Java读取文件性能优化程序代码资源
读取文件就是对代码与算法上进行了一些细节上的修改了,下文我们来看一段java读取文件性能优化的例子。

在执行IO时,Java的InputStream被广泛使用,比如DataInputStream.readInt等等。事实上,这些高度封装的接口奇慢无比。我有一个项目启动时需要读取90MB左右的词典文件,用DataInputStream耗时3秒以上,换用java.nio包直接操作内存字节,可以加速到300ms左右,整整提速10倍!当然,前提是你熟悉位运算。

java.nio中提供了两类 FileChannel 和 ByteBuffer来将文件映射到内存,其中FileChannel表示文件通道,ByteBuffer是一个缓冲区。

具体步骤

①从FileInputStream、FileOutputStream以及RandomAccessFile对象获取文件通道

②将文件内存映射到ByteBuffer

③通过byteBuffer.array()接口得到一个byte数组

④直接操作字节

示例代码

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9439')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9439> FileInputStream fis = new FileInputStream(path);
// 1.从FileInputStream对象获取文件通道FileChannel
FileChannel channel = fis.getChannel();
int fileSize = (int) channel.size();
 
// 2.从通道读取文件内容
ByteBuffer byteBuffer = ByteBuffer.allocate(fileSize);
 
// channel.read(ByteBuffer) 方法就类似于 inputstream.read(byte)
// 每次read都将读取 allocate 个字节到ByteBuffer
channel.read(byteBuffer);
// 注意先调用flip方法反转Buffer,再从Buffer读取数据
byteBuffer.flip();
// 可以将当前Buffer包含的字节数组全部读取出来
byte[] bytes = byteBuffer.array();
byteBuffer.clear();
// 关闭通道和文件流
channel.close();
fis.close();
 
int index = 0;
size = Utility.bytesHighFirstToInt(bytes, index);
index = 4;

其中,如果你当初使用了DataOutputStream.writeInt来保存文件的话,那么在读取的时候就要注意了。writeInt写入四个字节,其中高位在前,低位在后,所以将byte数组转为int的时候需要倒过来转换:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2825')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2825>

/**
     * 字节数组和整型的转换,高位在前,适用于读取writeInt的数据
     *
     * @param bytes 字节数组
     * @return 整型
     */
    public static int bytesHighFirstToInt(byte[] bytes, int start)
    {
        int num = bytes[start 3] & 0xFF;
        num |= ((bytes[start 2] << 8) & 0xFF00);
        num |= ((bytes[start 1] << 16) & 0xFF0000);
        num |= ((bytes[start] << 24) & 0xFF000000);
        return num;
    }

改变buffer的大小也可以起到使用

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4348')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4348>public static void copy1(File src, File dest) throws Exception {
  FileInputStream fileInputStream = null;
  FileOutputStream fileOutputStream = null;
  try {
   fileInputStream = new FileInputStream(src);
   fileOutputStream = new FileOutputStream(dest);
   byte[] buffer = new byte[8096];
   int length = -1;
   while((length = fileInputStream.read(buffer)) != -1) {
    fileOutputStream.write(buffer, 0, length);//一次性将缓冲区的所有数据都写出去
    fileOutputStream.flush();
   }
  } finally {
   if(fileInputStream != null) {
    fileInputStream.close();
   }
   if(fileOutputStream != null) {
    fileOutputStream.close();
   }
  }
 }

 

您可能感兴趣的文章:
php和java哪个好?
Java的网络功能与编程 一
php和java的优势
Java读取文件性能优化程序代码
经典Java线程面试题70道
javascript基础教程(1)-语言特点
Java内存模型和线程安全的使用例子
php和java差在哪里?
PHP常用缓存方式:
Java中基本数据类型与流的操作方法

[关闭]
~ ~