收納進(jìn)此專輯:I/O流官方中文指南系列概述及索引
大部分內(nèi)容來自 The Java? Tutorials 官方指南,其余來自別處如ifeve的譯文、imooc、書籍Android面試寶典等等酱虎。
作者: @youyuge
個人博客站點(diǎn): https://youyuge.cn
一、緩沖流Buffered Streams
Most of the examples we've seen so far use unbuffered I/O. This means each read or write request is handled directly by the underlying OS. This can make a program much less efficient, since each such request often triggers disk access, network activity, or some other operation that is relatively expensive.
目前為止擂涛,我們所使用的都是未緩沖的I/O逢净。這意味著每次的讀寫請求都直接通過底層的操作系統(tǒng)OS。這可能使得一個程序不那么有效率歼指,由于每次這樣的請求會經(jīng)常觸發(fā)硬盤的讀取爹土,網(wǎng)絡(luò)活動或者一些其他相當(dāng)耗時的操作。
To reduce this kind of overhead, the Java platform implements buffered I/O streams. Buffered input streams read data from a memory area known as a buffer; the native input API is called only when the buffer is empty. Similarly, buffered output streams write data to a buffer, and the native output API is called only when the buffer is full.
為了減少這種負(fù)載踩身,Java平臺實(shí)現(xiàn)了緩沖I/O流胀茵。緩沖輸入流從一個被稱為緩沖區(qū)的存儲區(qū)讀取數(shù)據(jù);僅僅當(dāng)緩沖區(qū)為空時挟阻,才會調(diào)用底層(native層)的輸入的api方法琼娘。同樣,緩沖輸出流輸出數(shù)據(jù)到緩沖區(qū)附鸽,僅當(dāng)緩沖區(qū)滿了之后才會調(diào)用底層的輸出方法(或者直接調(diào)用flush方法立刻輸出緩沖區(qū)內(nèi)容)脱拼。
A program can convert an unbuffered stream into a buffered stream using the wrapping idiom we've used several times now, where the unbuffered stream object is passed to the constructor for a buffered stream class. Here's how you might modify the constructor invocations in the CopyCharacters example to use buffered I/O:
Java程序能通過包裝構(gòu)造方法,把一個非緩沖流轉(zhuǎn)換成緩沖流坷备。只要將非緩沖流對象傳給緩沖流的類的構(gòu)造器即可熄浓。下面展示了如何通過構(gòu)造器包裝來使用緩沖流I/O:
inputStream = new BufferedReader(new FileReader("xanadu.txt"));
outputStream = new BufferedWriter(new FileWriter("characteroutput.txt"));
There are four buffered stream classes used to wrap unbuffered streams: BufferedInputStream
andBufferedOutputStream
create buffered byte streams, while BufferedReader
and BufferedWriter
create buffered character streams.
有四種緩沖流的類,來包裝非緩沖流:BufferedInputStream 和 BufferedOutputStream構(gòu)造緩沖字節(jié)流省撑, BufferedReader 和 BufferedWriter構(gòu)造緩沖字符流赌蔑。
二俯在、清空(立刻輸出)緩沖流Flushing Buffered Streams
It often makes sense to write out a buffer at critical points, without waiting for it to fill. This is known as flushing the buffer.
非常多的情況下,有必要在一些關(guān)鍵時刻確保緩沖區(qū)的及時輸出娃惯,而非等待它填滿再輸出跷乐。這被稱為清空緩沖區(qū)。
Some buffered output classes support autoflush, specified by an optional constructor argument. When autoflush is enabled, certain key events cause the buffer to be flushed. For example, an autoflush PrintWriter
object flushes the buffer on every invocation of println
or format
. See Formatting for more on these methods.
一些緩沖輸出類支持自動清空緩沖區(qū)趾浅,由可選的構(gòu)造器參數(shù)來確定愕提。當(dāng)自動清空可用時,一些明確規(guī)定好的關(guān)鍵事件會觸發(fā)緩沖區(qū)的清空皿哨。舉個栗子揪荣,一個自動清空的PrintWriter對象在每次調(diào)用println或者format方法時會自動清空輸出緩沖區(qū)。(比如我們熟知的System.out.println()方法)
To flush a stream manually, invoke its flush method. The flush method is valid on any output stream, but has no effect unless the stream is buffered.
想人為地清空一個流往史,可以調(diào)用它的flush()方法仗颈。這個方法在任何的輸出流上都是可用的,但是僅僅在緩沖流上有效果椎例。