java filechannel inputstream,三种读取文件的方式(inputStream,BufferedInputStream,FileChannel)...

package com.qzl.learn;

import javax.annotation.processing.SupportedSourceVersion;

import java.io.*;

import java.nio.channels.Channels;

import java.nio.channels.FileChannel;

import java.nio.channels.WritableByteChannel;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class IOTestMain {

public static void main (String s[]){

IOTestMain ioTestMain = new IOTestMain();

ioTestMain.readFileTest();

}

public void readFileTest(){

long time = System.currentTimeMillis();

File zipFile = new File("C:\\projects\\dbdatafile\\test.rar");

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;

try{

fileOutputStream = new FileOutputStream(zipFile);

zipOutputStream = new ZipOutputStream(fileOutputStream);

for ( int i = 0;i<10;i++){

File in = new File("C:\\projects\\dbdatafile\\test.txt");

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);

inputStream.close();

inputStream = null;

}

zipOutputStream.closeEntry();

zipOutputStream.finish();

}catch (Exception e){

System.out.println("1:"+e.getStackTrace());

}finally {

try{

if (fileOutputStream != null ){

fileOutputStream.close();

}

}catch (Exception e){

System.out.println("2:"+e.getStackTrace());

}

try{

if (zipOutputStream != null){

zipOutputStream.close();

}

}catch (Exception e){

System.out.println("3:"+e.getStackTrace());

}

try{

if (bufferedOutputStream != null){

bufferedOutputStream.close();

}

}catch (Exception e){

System.out.println("3.1:"+e.getStackTrace());

}

try{

if (writableByteChannel != null && writableByteChannel.isOpen()){

writableByteChannel.close();

}

}catch (Exception e){

System.out.println("3.2:"+e.getStackTrace());

}

try{

if (inputStream != null){

inputStream.close();

}

}catch (Exception e){

System.out.println("4:"+e.getStackTrace());

}

try{

if (bufferedInputStream != null){

bufferedInputStream.close();

}

}catch (Exception e){

System.out.println("5:"+e.getStackTrace());

}

try{

if (fileChannel != null && fileChannel.isOpen()){

fileChannel.close();

}

}catch (Exception e){

System.out.println("5:"+e.getStackTrace());

}

}

printTimeCost(time);

}

private void printTimeCost(long timestart){

long timeNow = System.currentTimeMillis();

System.out.println("cost Time is : " + (timeNow -timestart));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值