几种读取文件的方式和耗时(inputStream,BufferedInputStream,FileChannel, directMemory, pipe)

public class IOTestMain {
public static void main (String s[]){
IOTestMain ioTestMain = new IOTestMain();
ioTestMain.readFileTest();
}

private void closeOutStream (OutputStream stream){
if (stream != null){
try{
stream.close();
}catch (IOException e){
System.out.println(e.getMessage());
}

}
}
private void closeInStream (InputStream inputStream){
if (inputStream != null){
try{
inputStream.close();
}catch (IOException e){
System.out.println(e.getMessage());
}

}
}
//异步任务
private void runTask(Pipe pipe,long fileLength,File in) {
try(ZipOutputStream zos = new ZipOutputStream(Channels.newOutputStream(pipe.sink()));
WritableByteChannel out = Channels.newChannel(zos)) {
for (int i = 0; i < 10; i++) {
//System.out.println("Begin"+i);
zos.putNextEntry(new ZipEntry("test"+i+".txt"));
FileChannel jpgChannel = new FileInputStream(in).getChannel();
jpgChannel.transferTo(0, fileLength, out);
jpgChannel.close();
}
zos.closeEntry();
//zos.finish();
}catch (Exception e){
e.printStackTrace();
}
}

public void readFileTest(){
long time = System.currentTimeMillis();
File zipFile = new File("C:\\projects\\dbdatafile\\test.rar");
File in = new File("C:\\projects\\dbdatafile\\test.txt");
ZipOutputStream zipOutputStream = null;
FileOutputStream fileOutputStream = null;
InputStream inputStream = null;
///use buffer
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;

//use file channel
FileChannel fileChannel = null;
WritableByteChannel writableByteChannel = null;

MappedByteBuffer mappedByteBuffer = null;
try{
fileOutputStream = new FileOutputStream(zipFile);

//第五种 异步方式耗时1.5+ S
writableByteChannel = Channels.newChannel(fileOutputStream);
Pipe pipe = Pipe.open();
CompletableFuture.runAsync(() ->runTask(pipe,in.length(),in));
ReadableByteChannel readableByteChannel = pipe.source();
ByteBuffer buffer = ByteBuffer.allocate(((int) in.length()*10));
while (readableByteChannel.read(buffer)>= 0) {
buffer.flip();
writableByteChannel.write(buffer);
buffer.clear();
}
if (writableByteChannel != null && writableByteChannel.isOpen()){
writableByteChannel.close();//如果不close,后面又直接重新new,会导致数据写不到磁盘
}
for ( int i = 0;i<10;i++){
if (zipOutputStream == null){
zipOutputStream = new ZipOutputStream(fileOutputStream);
}
inputStream = new FileInputStream(in);
bufferedInputStream = new BufferedInputStream(inputStream);
zipOutputStream.putNextEntry(new ZipEntry("test"+i+".txt"));
bufferedOutputStream = new BufferedOutputStream(zipOutputStream);
fileChannel = ((FileInputStream) inputStream).getChannel();
writableByteChannel = Channels.newChannel(zipOutputStream);
int temp = 0;
//第一种方式耗时间20+秒
while ((temp = inputStream.read())!= -1){
zipOutputStream.write(temp);
}
//第二种方式耗时间0.8+秒
while ((temp = bufferedInputStream.read())!= -1){
bufferedOutputStream.write(temp);
}
//第三种方式耗时间0.4+秒
fileChannel.transferTo(0,in.length(),writableByteChannel);
//第四种,使用java堆外内存做缓冲区,耗时0.4+秒
mappedByteBuffer =
new RandomAccessFile(in,"r").getChannel().map(FileChannel.MapMode.READ_ONLY,0,in.length());
writableByteChannel.write(mappedByteBuffer);
closeInStream(inputStream);
inputStream = null;
}
closeOutStream(zipOutputStream);
zipOutputStream = null;
}catch (Exception e){
System.out.println("1:"+e.getStackTrace()+e.getMessage());
}finally {
closeOutStream(fileOutputStream);
closeOutStream(zipOutputStream);
closeOutStream(bufferedOutputStream);
try{
if (writableByteChannel != null && writableByteChannel.isOpen()){
writableByteChannel.close();
}
}catch (Exception e){
System.out.println("3.2:"+e.getStackTrace());
}
closeInStream(inputStream);
closeInStream(bufferedInputStream);
try{
if (fileChannel != null && fileChannel.isOpen()){
fileChannel.close();
}
}catch (Exception e){
System.out.println("6:"+e.getStackTrace());
}

}
printTimeCost(time);
}
private void printTimeCost(long timestart){
long timeNow = System.currentTimeMillis();
System.out.println("cost Time is : " + (timeNow -timestart));
}

}

posted @ 2019-11-29 14:48  thinkqin  阅读(1377)  评论(0编辑  收藏  举报