其他分享
首页 > 其他分享> > 三种读取文件的方式(inputStream,BufferedInputStream,FileChannel)

三种读取文件的方式(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));
}
}

标签:zipOutputStream,BufferedInputStream,System,inputStream,FileChannel,println,new,n
来源: https://www.cnblogs.com/thinkqin/p/11957720.html