ReadFile2ByteArrayTest

1996 단어 자바
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);
}
}

좋은 웹페이지 즐겨찾기