亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

關于BufferedOutStream 到底怎么寫文件的

https://img1.sycdn.imooc.com//5ba8527500015d3006050225.jpg

視頻中不是說

?FileOutputStream-->write()方法相當于一滴一滴地把水"轉移"過去

?DataOutputStream-->wtireXxx()方相當于一瓢一瓢把水"轉移"過去

?BufferedOutputStream-->write方法相當于一瓢一瓢先放入桶中,在從桶中倒入到另一個缸中,性能提高


那按照while中的代碼,怎么還是感覺是一個字節一個字節讀的,沒有體現出瓢和桶的概念?。坎欢?/p>

正在回答

4 回答

FileOutputStream的write(int)是直接把字節寫到磁盤文件上,相當于直接從這個山頭的缸中取了一滴水,然后爬到另一個山頭放入那個缸中。

FileOutputStream的write(byte[])是直接把字節先寫到字節數組中,然后統一寫到磁盤文件上,相當于直接從這個山頭的缸中舀一瓢水,然后爬到另一個山頭倒入那個缸中。

DataOutputStream的writeXxx()理解跟FileOutputStream的write(byte[])差不多

BufferedOutputStream的write(int)方法相當于把字節一個一個放入一個緩沖區中,再一次性將緩沖區中的內容寫到磁盤文件上。相當于將這個山頭的水缸中的水一滴一滴放入鐵桶中,當這個水缸的水放完了,你再抱著這個鐵桶一次性的將水倒入另一個山頭的缸中。

BufferedOutputStream的write(byte[])方法是最牛逼的,把字節一部分一部分的拿出,每次拿出都放入字節數組,再將字節數組放入緩沖區中,再一次性將緩沖區中的內容寫到磁盤文件上。相當于從這個山頭的水缸中一瓢一瓢的舀水放入鐵桶中,當這個水缸舀完了,你再抱著這個鐵桶一次性的將水倒入另一個山頭的缸中。

你說哪個方法走的路少比較輕松? 下面是我的代碼,DataOutputStream的沒有寫。你比較下

package com.imooc.io;

import java.io.*;

public class IOUtil {

public static void copyFileByByte(File sourceFile,File destinationFile) throws IOException{

if( !sourceFile.exists() ) {

throw new IllegalArgumentException(sourceFile+"文件不存在");

}

if( !sourceFile.isFile() ) {

throw new IllegalArgumentException(sourceFile+"不是文件");

}

FileInputStream fis=new FileInputStream(sourceFile);

FileOutputStream fos=new FileOutputStream(destinationFile);

int b;

while( (b=fis.read())!=-1 ) {

fos.write(b);

fos.flush(); //最好加上

}

fis.close();

fos.close();

}

public static void copyFileByByteArray(File sourceFile,File destinationFile) throws IOException{

if( !sourceFile.exists() ) {

throw new IllegalArgumentException(sourceFile+"文件不存在");

}

if( !sourceFile.isFile() ) {

throw new IllegalArgumentException(sourceFile+"不是文件");

}

FileInputStream fis=new FileInputStream(sourceFile);

FileOutputStream fos=new FileOutputStream(destinationFile);

byte[] buf=new byte[16*1024];

int b;

while( (b=fis.read(buf,0,buf.length))!=-1 ) {

fos.write(buf,0,b);

fos.flush(); //最好加上

}

fis.close();

fos.close();

}

public static void copyFileByBufferByByte(File sourceFile,File destinationFile) throws IOException{

if( !sourceFile.exists() ) {

throw new IllegalArgumentException(sourceFile+"文件不存在");

}

if( !sourceFile.isFile() ) {

throw new IllegalArgumentException(sourceFile+"不是文件");

}

BufferedInputStream bis=new BufferedInputStream( new FileInputStream(sourceFile) );

BufferedOutputStream bos=new BufferedOutputStream( new FileOutputStream(destinationFile) );

int b=0;

while( (b=bis.read())!=-1 ) {

bos.write(b);

}

bos.flush();

bis.close();

bos.close();

}

public static void copyFileByBufferByByteArray(File sourceFile,File destinationFile) throws IOException{

if( !sourceFile.exists() ) {

throw new IllegalArgumentException(sourceFile+"文件不存在");

}

if( !sourceFile.isFile() ) {

throw new IllegalArgumentException(sourceFile+"不是文件");

}

BufferedInputStream bis=new BufferedInputStream( new FileInputStream(sourceFile) );

BufferedOutputStream bos=new BufferedOutputStream( new FileOutputStream(destinationFile) );

byte[] buf=new byte[16*1024];

int b=0;

while( (b=bis.read(buf,0,buf.length))!=-1 ) {

bos.write(buf,0,buf.length);

}

bos.flush();

bis.close();

bos.close();

}

}


3 回復 有任何疑惑可以回復我~

同問 有人能解答嗎

0 回復 有任何疑惑可以回復我~

他們提供的方法

read 或者write 是一個一個字節的讀寫;

readXxx 或者writeXxx是一次性寫入

readline或者writerline 是一行一行的讀寫


0 回復 有任何疑惑可以回復我~

read 或者write 是一個一個字節的讀寫;

readXxx 或者writeXxx是一次性寫入

readline或者writerline 是一行一行的讀寫

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
文件傳輸基礎——Java IO流
  • 參與學習       133821    人
  • 解答問題       1060    個

為您介紹IO流的使用,以及對象的序列化和反序列化的內容

進入課程

關于BufferedOutStream 到底怎么寫文件的

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號