Three ways to read the file (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);
= new new BufferedInputStream BufferedInputStream (inputStream);
ZipOutputStream.putNextEntry (the ZipEntry new new ( "Test" + I +. "TXT"));
BufferedOutputStream BufferedOutputStream The new new = (ZipOutputStream);
FileChannel = ((the FileInputStream) inputStream) .getChannel ();
WritableByteChannel = Channels.newChannel (ZipOutputStream);
int TEMP = 0;
// with one of three ways can be a
// a first embodiment elapsed time 20+ seconds
while (! (temp = inputStream.read ( )) = -1) {
zipOutputStream.write (TEMP);
}
// second embodiment 0.8+ seconds elapsed time
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));
}
}

Guess you like

Origin www.cnblogs.com/thinkqin/p/11957720.html