`
f543711700
  • 浏览: 322710 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

ReadFile2ByteArrayTest

 
阅读更多
package com.ghca.policeintf.test;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * Created by DUDU on 2017/11/2.
 */
public class ReadFile2ByteArrayTest {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("D:\\32位系统移植过来的文件\\1`.rar");
        byte[] buffer = new byte[1024];
        int totalReadCount = 0;
        while(true) {
            int remaining = buffer.length - totalReadCount;
//                if (remaining > 0) {
//                    int currentReadCount = fis.read(buffer,totalReadCount,remaining);
//                    if (currentReadCount > 0) {
//                        totalReadCount = totalReadCount +  currentReadCount;
//                    } else {
//                        break;
//                    }
//                } else {//扩容buffer
//                    byte[] newBuffer = new byte[buffer.length * 2];
//                    System.arraycopy(buffer,0,newBuffer,0,totalReadCount);
//                    buffer = newBuffer;
//                }
            if (remaining <= 0) {//先扩容buffer
                byte[] newBuffer = new byte[buffer.length * 2];
                System.arraycopy(buffer,0,newBuffer,0,buffer.length);
                buffer = newBuffer;
//                remaining = buffer.length - totalReadCount;
                continue;
            }
            int currentReadCount =  fis.read(buffer,totalReadCount,remaining);
            if (currentReadCount > 0) {
                totalReadCount = totalReadCount + currentReadCount;
            } else {
                break;
            }
        }
        System.out.println("总共读取字节数:" + totalReadCount);
        System.out.println("buffer大小:" + buffer.length);
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics