第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

關(guān)于BufferedOutStream 到底怎么寫文件的

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

視頻中不是說

?FileOutputStream-->write()方法相當(dāng)于一滴一滴地把水"轉(zhuǎn)移"過去

?DataOutputStream-->wtireXxx()方相當(dāng)于一瓢一瓢把水"轉(zhuǎn)移"過去

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


那按照while中的代碼,怎么還是感覺是一個(gè)字節(jié)一個(gè)字節(jié)讀的,沒有體現(xiàn)出瓢和桶的概念???不懂

正在回答

4 回答

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

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

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

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

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

你說哪個(gè)方法走的路少比較輕松? 下面是我的代碼,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 回復(fù) 有任何疑惑可以回復(fù)我~

同問 有人能解答嗎

0 回復(fù) 有任何疑惑可以回復(fù)我~

他們提供的方法

read 或者write 是一個(gè)一個(gè)字節(jié)的讀寫;

readXxx 或者writeXxx是一次性寫入

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


0 回復(fù) 有任何疑惑可以回復(fù)我~

read 或者write 是一個(gè)一個(gè)字節(jié)的讀寫;

readXxx 或者writeXxx是一次性寫入

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

0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消
文件傳輸基礎(chǔ)——Java IO流
  • 參與學(xué)習(xí)       133811    人
  • 解答問題       1058    個(gè)

為您介紹IO流的使用,以及對(duì)象的序列化和反序列化的內(nèi)容

進(jìn)入課程

關(guān)于BufferedOutStream 到底怎么寫文件的

我要回答 關(guān)注問題
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)