A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

今天学习BufferedReader与BufferedWriter。

BufferedReader,字符缓冲输入流,作用是为其他输入流提供缓冲功能。BufferedReader从其他字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。

通常,Reader所作的每个读取请求都会导致对底层字符或字节流进行相应的读取请求。因此,建议用BufferedReader包装所有其read()操作可能开销很高的Reader(如FileReader和InputStreamReader)。例如,
BufferedReader in = new BufferedReader(new FileReader("foo.in"));将缓冲指定文件的输入。如果没有缓冲,则每次调用read()或readLine()都会导致从文件中读取字节,并将其转换为字符后返回,而这是极其低效的。

BufferedWriter,字符缓冲输出流,作用是为其他输出流提供缓冲功能。BufferedWriter将文本写入其他字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。

通常Writer将其输出立即发送到底层字符或字节流。除非要求提示输出,否则建议用BufferedWriter包装所有其write()操作可能开销很高的 Writer(如FileWriters和OutputStreamWriters)。例如,PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("foo.out"))); 将缓冲PrintWriter对文件的输出。如果没有缓冲,则每次调用print()方法会导致将字符转换为字节,然后立即写入到文件,而这是极其低效的。

BufferedReader

为了更好地理解BufferedReader的源码,我们先来了解其思想。BufferedReader是如何为其他输入流提供缓冲功能的?提供缓冲为什么能实现字符、数组和行的高效读取?

先来看看第一次问题,BufferedReader是如何为其他输入流提供缓冲功能的?

创建BufferReader时,我们会通过它的构造函数指定某个Reader为参数。BufferedReader将Reader中的数据分批取到自己的buffer中(内置缓存字符数组),然后处理buffer中的数据。操作完buffer中的数据后,BufferedReader会从Reader中读取下一批数据到buffer中供程序处理。如此循环往复,直到Reader中数据被读取完毕。这其中就有个关键的方法fill(),每当buffer中数据被读取完之后,fill()就会从Reader中将数据填充到buffer中。这个方法下面会仔细讲解。

提供缓冲为什么能实现字符、数组和行的高效读取?

一是提高了读取的效率、二是减少了打开存储介质的连接次数。缓冲中的数据实际上是保存在内存中,而原始数据可能是保存在硬盘中。从内存中读取数据的速度比从硬盘读取数据的速度至少快10倍以上。

到这里又有个问题,为什么不一次性将Reader中全部数据都读取到缓冲中呢?

第一,读取全部的数据所需要的时间可能会很长。第二,内存价格很贵,容量远没有硬盘那么大。我们能做的就是在效率和成本之间找到平衡点。大多数情况下,默认值就足够大了。

下面来学习下BufferedReader的源码,看看BufferedReader是如何为其他输入流提供缓冲功能,实现字符、数组和行的高效读取的。

public class BufferedReader extends Reader {    // 底层字符输入流    private Reader in;    // 字符缓冲区    private char cb[];    // nChars是cb字符缓冲区中字符的总的个数    // nextChar是下一个要读取的字符在cb缓冲区中的位置    private int nChars, nextChar;    // 表示标记无效。设置了标记,但是被标记位置由于某种原因导致标记无效    private static final int INVALIDATED = -2;    //表示没有标记    private static final int UNMARKED = -1;    //标记位置初始化为UNMARKED    private int markedChar = UNMARKED;    //在仍保留该标记的情况下,对可读取字符数量的限制。    //在读取达到或超过此限制的字符后,尝试重置流可能会失败。    //限制值大于输入缓冲区的大小将导致分配一个新缓冲区,其大小不小于该限制值。因此应该小心使用较大的值。    private int readAheadLimit = 0; /* Valid only when markedChar > 0 */    //表示是否跳过换行符。(skipLF ,skip line feed)    private boolean skipLF = false;    //表示当做了标记时,是否忽略换行符。    private boolean markedSkipLF = false;    //字符缓冲区默认大小    private static int defaultCharBufferSize = 8192;    //每行默认的字符个数    private static int defaultExpectedLineLength = 80;    /**     * 创建指定底层字符输入流in和指定字符缓冲区大小sz的BufferedReader     */    public BufferedReader(Reader in, int sz) {        super(in);        if (sz <= 0)            throw new IllegalArgumentException("Buffer size <= 0");        this.in = in;        cb = new char[sz];        nextChar = nChars = 0;    }    /**     * 创建指定底层字符输入流in和默认字符缓冲区大小defaultCharBufferSize的BufferedReader     */    public BufferedReader(Reader in) {        this(in, defaultCharBufferSize);    }    /**      * 检查BufferedReader是否处于open状态。     * 如果in不为null,BufferedReader即为open状态。     */    private void ensureOpen() throws IOException {        if (in == null)            throw new IOException("Stream closed");    }    /**     * 填充缓冲区。     * 如果标记有效,要考虑标记。     */    private void fill() throws IOException {        //cb中填充数据的起始位置        int dst;        //如果没有标记,从缓冲区索引为0的位置开始填充        if (markedChar <= UNMARKED) {            dst = 0;        } else {//如果有标记            //delta为标记位置与下个读取字符之间的距离            int delta = nextChar - markedChar;            //如果delta超出readAheadLimit,标记即为无效。            if (delta >= readAheadLimit) {                 markedChar = INVALIDATED;                readAheadLimit = 0;                dst = 0;            } else {                //如果delta没有超出readAheadLimit,即标记有效                //且readAheadLimit小于等于缓冲区长度                //将markedChar与nextChar之间的字符写入到缓冲区中                if (readAheadLimit <= cb.length) {                    /* Shuffle in the current buffer */                    System.arraycopy(cb, markedChar, cb, 0, delta);                    markedChar = 0;                    dst = delta;                } else {                    //如果delta没有超出readAheadLimit,即标记有效                    //且readAheadLimit大于缓冲区长度                    //将重新设置缓冲区大小,markedChar与nextChar之间的字符写入到缓冲区中                    char ncb[] = new char[readAheadLimit];                    System.arraycopy(cb, markedChar, ncb, 0, delta);                    cb = ncb;                    markedChar = 0;                    dst = delta;                }                nextChar = nChars = delta;            }        }        int n;        //从底层输入流中读取数据,并存储到缓冲区b中        //如果没有读取到数据,就继续读,直到读到数据或者到达流末尾为止        do {            n = in.read(cb, dst, cb.length - dst);        } while (n == 0);        //如果读到了数据        if (n > 0) {            nChars = dst + n;            nextChar = dst;        }    }    /**     * 读取单个字符。     *     * @return 作为一个整数(其范围从0到65535( 0x00-0xffff))返回,如果已到达流末尾,则返回 -1     */    public int read() throws IOException {        synchronized (lock) {            //确认BufferedReader是否处于开启状态            ensureOpen();            //???            for (;;) {                //如果缓冲区数据已被读完,填充缓冲区。如果填充缓冲区后缓冲区依然是空的,说明已到达流末尾,返回-1。                if (nextChar >= nChars) {                    fill();                    if (nextChar >= nChars)                        return -1;                }                //如果skipLF为true,说明要跳过换行符                if (skipLF) {                    //先将skipLF置为false。???                    skipLF = false;                    //如果缓冲区内下个字符为换行符,跳过它。                    if (cb[nextChar] == '\n') {                        nextChar++;                        continue;                    }                }                //返回缓冲区中下个字符,然后nextChar+1                return cb[nextChar++];            }        }    }    /**     * 从缓冲区中读取数据,写入到cbuf中。off为开始存储字符处的偏移量。len为要读取的最大字符数。     * 如有必要,从底层输入流中读取数据。     *      * 该方法在read(char cbuf[], int off, int len)中被调用     *      * @param   b     目标字符数组     * @param   off   开始存储字符处的偏移量     * @param   len   要读取的最大字符数     * @return  实际读入cbuf的总字节数,如果由于已到达流末尾而不再有数据,则返回-1。     */    private int read1(char[] cbuf, int off, int len) throws IOException {        //如果缓冲区已被读完        if (nextChar >= nChars) {            //如果要读取的长度大于等于缓冲区大小,且没有标记,且不跳过换行符            if (len >= cb.length && markedChar <= UNMARKED && !skipLF) {                //直接从底层输入流中读取数据到cbuf中                return in.read(cbuf, off, len);            }            //填充缓冲区            fill();        }        //如果以上步骤执行完后,缓冲区还是没有可读数据,说明已经到达流末尾,返回-1        if (nextChar >= nChars) return -1;        //如果跳过换行符        if (skipLF) {            //将skipLF置为false。why???            skipLF = false;            //如果下个字符是换行符            if (cb[nextChar] == '\n') {                //跳过它                nextChar++;                //如果缓冲区已被读完,填充缓冲区                if (nextChar >= nChars)                    fill();                //如果填充缓冲区后,缓冲区依然是空的。说明已经到达流末尾,返回-1                if (nextChar >= nChars)                    return -1;            }        }        //计算实际应该读取的字节数        int n = Math.min(len, nChars - nextChar);        //读取        System.arraycopy(cb, nextChar, cbuf, off, n);        //缓冲区当前位置+n        nextChar += n;        return n;    }    /**     * 从缓冲区中读取数据,写入到cbuf中。off为开始存储字符处的偏移量。len为要读取的最大字符数。     * 如有必要,从底层输入流中读取数据。     *      * @param   b     目标字符数组     * @param   off   开始存储字符处的偏移量     * @param   len   要读取的最大字符数     * @return  实际读入cbuf的总字节数,如果由于已到达流末尾而不再有数据,则返回-1。     */    public int read(char cbuf[], int off, int len) throws IOException {        synchronized (lock) {            ensureOpen();            //判断参数是否合法            if ((off < 0) || (off > cbuf.length) || (len < 0) ||                ((off + len) > cbuf.length) || ((off + len) < 0)) {                throw new IndexOutOfBoundsException();            } else if (len == 0) {                return 0;            }            //如果参数合法,调用read1(char[] cbuf, int off, int len)方法读取数据            int n = read1(cbuf, off, len);            //如果没有读取到数据,直接返回读取结果            if (n <= 0) return n;            //当没有读取到len个字符,且还可以继续读取            while ((n < len) && in.ready()) {                //继续尝试读取                int n1 = read1(cbuf, off + n, len - n);                if (n1 <= 0) break;                n += n1;            }            return n;        }    }    /**     * 读取一行字符。     * 通过下列字符之一即可认为某行已终止:换行 ('\n')、回车 ('\r') 或回车后直接跟着换行。     *     * @param      ignoreLF  是否忽略换行符。如果为true,遇到换行符时,跳过它。     *     * @return     包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回     * @see        java.io.LineNumberReader#readLine()     * @exception  IOException  If an I/O error occurs     */    String readLine(boolean ignoreLF) throws IOException {        StringBuffer s = null;        int startChar;        synchronized (lock) {            ensureOpen();            boolean omitLF = ignoreLF || skipLF;        bufferLoop:            for (;;) {                if (nextChar >= nChars)                    fill();                if (nextChar >= nChars) { /* EOF */                    if (s != null && s.length() > 0)                        return s.toString();                    else                        return null;                }                boolean eol = false;                char c = 0;                int i;                /* Skip a leftover '\n', if necessary */                if (omitLF && (cb[nextChar] == '\n'))                    nextChar++;                skipLF = false;                omitLF = false;            charLoop:                for (i = nextChar; i < nChars; i++) {                    c = cb;                    if ((c == '\n') || (c == '\r')) {                        eol = true;                        break charLoop;                    }                }                startChar = nextChar;                nextChar = i;                if (eol) {                    String str;                    if (s == null) {                        str = new String(cb, startChar, i - startChar);                    } else {                        s.append(cb, startChar, i - startChar);                        str = s.toString();                    }                    nextChar++;                    if (c == '\r') {                        skipLF = true;                    }                    return str;                }                if (s == null)                    s = new StringBuffer(defaultExpectedLineLength);                s.append(cb, startChar, i - startChar);            }        }    }    /**     * 读取一个文本行。     * 通过下列字符之一即可认为某行已终止:换行('\n')、回车('\r')或回车后直接跟着换行。     *     * @return    包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回null     */    public String readLine() throws IOException {        return readLine(false);    }    /**     * 跳过字符。     *     * @param  n  要跳过的字符数     *     * @return    实际跳过的字符数     */    public long skip(long n) throws IOException {        if (n < 0L) {            throw new IllegalArgumentException("skip value is negative");        }        synchronized (lock) {            ensureOpen();            long r = n;            while (r > 0) {                //如果缓冲区中已无数据可读,填充缓冲区                if (nextChar >= nChars)                    fill();                //如果依然没有数据可读,说明已到达流末尾,没有字符可以跳了,结束循环                if (nextChar >= nChars) /* EOF */                    break;                //能运行到这里,说明有字符可跳。                if (skipLF) {                    skipLF = false;                    if (cb[nextChar] == '\n') {                        nextChar++;                    }                }                //d为缓冲区中剩余可读的字符数                long d = nChars - nextChar;                //如果r<=d,即n<=d,即要跳过的字符数小于等于缓冲区中剩余可读的字符数。直接跳过r个字符即可                if (r <= d) {                    nextChar += r;                    r = 0;                    break;                }//如果要跳过的字符数大于缓冲区中剩余可读的字符数,                //跳过可跳的n个字符,此时缓冲区没有数据可读                else {                    r -= d;//r为剩下需要跳过的字符数。                    nextChar = nChars;//跳过可跳的n个字符,此时缓冲区没有数据可读                }                //此时,一次循环就结束了。下次循环会判断r是否大于0,如果大于0,继续循环。            }            //返回实际跳过的字符数。            return n - r;        }    }    /**     * 判断此流是否已准备好被读取。     * 如果缓冲区不为空,或者底层字符流已准备就绪,则缓冲的字符流准备就绪。     */    public boolean ready() throws IOException {        synchronized (lock) {            ensureOpen();            //如果需要跳过换行符,且下个字符是换行符,那就跳过它            if (skipLF) {                //如果缓冲区已被读完且底层缓冲区已准备好被读取,就填充缓冲区                if (nextChar >= nChars && in.ready()) {                    fill();                }                //如果缓冲区还有数据可读                if (nextChar < nChars) {                    //跳过换行符                    if (cb[nextChar] == '\n')                        nextChar++;                    //why???                    skipLF = false;                }            }            //如果缓冲区不为空,或者底层字符流已准备就绪,则缓冲的字符流准备就绪。            return (nextChar < nChars) || in.ready();        }    }    /**     * 判断此流是否支持mark()操作     */    public boolean markSupported() {        return true;    }    /**     * 标记流中的当前位置。     * 对reset()的后续调用将尝试将该流重新定位到此点。     */    public void mark(int readAheadLimit) throws IOException {        if (readAheadLimit < 0) {            throw new IllegalArgumentException("Read-ahead limit < 0");        }        synchronized (lock) {            ensureOpen();            this.readAheadLimit = readAheadLimit;            markedChar = nextChar;            markedSkipLF = skipLF;        }    }    /**     * 将流重置到最新的标记。     */    public void reset() throws IOException {        synchronized (lock) {            ensureOpen();            if (markedChar < 0)                throw new IOException((markedChar == INVALIDATED)                                      ? "Mark invalid"                                      : "Stream not marked");            nextChar = markedChar;            skipLF = markedSkipLF;        }    }    //关闭该流并释放与之关联的所有资源。    public void close() throws IOException {        synchronized (lock) {            if (in == null)                return;            try {                in.close();            } finally {                in = null;                cb = null;            }        }    }    /**     * 返回一个Stream,其中的元素是从BufferedReader中读出的行。     *     * @since 1.8     */    public Stream<String> lines() {        Iterator<String> iter = new Iterator<String>() {            String nextLine = null;            @Override            public boolean hasNext() {                if (nextLine != null) {                    return true;                } else {                    try {                        nextLine = readLine();                        return (nextLine != null);                    } catch (IOException e) {                        throw new UncheckedIOException(e);                    }                }            }            @Override            public String next() {                if (nextLine != null || hasNext()) {                    String line = nextLine;                    nextLine = null;                    return line;                } else {                    throw new NoSuchElementException();                }            }        };        return StreamSupport.stream(Spliterators.spliteratorUnknownSize(                iter, Spliterator.ORDERED | Spliterator.NONNULL), false);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
demoimport java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import org.junit.Test;/** * BufferedReader Demo */public class BufferedReaderTest {    @Test    public void test() {        try {            File file = new File("bufferedReader.txt");            BufferedReader in = new BufferedReader(new FileReader(file));            System.out.println("从缓冲区中读取5个字符,并打印");            for (int i = 0; i < 5; i++) {                if (in.ready()) {                    int tmp = in.read();                    System.out.println(i + ":" + (char) tmp);                }            }            char[] buf = new char[3];            in.read(buf, 0, 3);            System.out.println("从缓冲区中读取三个字符存到字符数组中,字符数组内容为" + String.valueOf(buf));            System.out.println("当前行剩余字符为" + in.readLine());            System.out.println("测试BufferedReader是否支持标记");            if (!in.markSupported()) {                System.out.println("make not supported!");                return;            }            System.out.println("make supported!");            System.out.println("测试标记功能(当前位置的下个字符为A)");            in.mark(1024);            System.out.println("跳过22个字符");            in.skip(22);            System.out.println("当前位置为" + (char) in.read());            System.out.println("重置输入流的索引为mark()所标记的位置,即重置到f处");            in.reset();            System.out.println("当前位置为" + (char) in.read());            in.close();        } catch (IOException e) {            e.printStackTrace();        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

bufferedReader.txt内容为

abcdefghijkABCDEFGHIJK1234124254?$%^&*(*^%
  • 1
  • 2
  • 3
  • 4

运行程序后,控制台打印内容为

从缓冲区中读取5个字符,并打印0:a1:b2:c3:d4:e从缓冲区中读取三个字符存到字符数组中,字符数组内容为fgh当前行剩余字符为ijk测试BufferedReader是否支持标记make supported!测试标记功能(当前位置的下个字符为A)跳过22个字符当前位置为4重置输入流的索引为mark()所标记的位置,即重置到f处当前位置为A
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
BufferedWriter

为了更好地理解BufferedWriter的源码,我们先来了解其思想。BufferedWriter是如何为其他输出流提供缓冲功能的?提供缓冲为什么能实现单个字符、数组和字符串的高效写入?

先来看看第一次问题,BufferedWriter是如何为其他输出流提供缓冲功能的?

创建BufferedWriter时,我们会通过它的构造函数指定某个底层字符输出流Writer为参数。当程序中每次将单个字符、数组和字符串写入到BufferedWriter中时、都会检查BufferedWriter中的缓存区是否存满,如果没有存满则将字符写入到缓存区中;如果存满,则调用底层的writer(char[] b, int off, int len)将缓存区中的所有字符一次性写入到底层Writer中。

提供缓冲为什么能实现单个字符、数组和字符串的高效写入?

如果没有缓冲,使用底层字符输出流向目的文件中写入单个字符、数组和字符串时,每写入一次就要打开一次到目的文件的连接。这样频繁的访问效率非常底下。比如,将一个非常大的数据写入到目的文件中,如果每写入一个字符就要打开一次到目的文件的连接,可以想象效率是如何低下。

有了缓冲,当使用底层字符输出流向目的文件中写入单个字符、数组和字符串时,将单个字符、数组和字符串先写入到BufferedWriter的内置缓存空间中,然后当达到一定数量时一次性写入Writer流中。此时,Writer就可以打开一次通道,将这个数据块写入到文件中。这样虽然不能达到一次访问就将所有数据写入磁盘中的效果,但也大大提高了效率和减少了对磁盘的访问量。

下面来学习下BufferedWriter的源码,看看BufferedWriter是如何为其他输出流提供缓冲功能,实现单个字符、数组和字符串的高效写入的。

public class BufferedWriter extends Writer {    //底层字符输出流    private Writer out;    //缓冲区    private char cb[];    //缓冲区大小。缓冲区当前位置的下个字符。    private int nChars, nextChar;    //默认字符缓冲区大小    private static int defaultCharBufferSize = 8192;    /** 行分隔符 **/    private String lineSeparator;    /**     * 创建一个使用默认大小输出缓冲区的缓冲字符输出流。 */    public BufferedWriter(Writer out) {        this(out, defaultCharBufferSize);    }    /**     * 创建一个使用给定大小输出缓冲区的新缓冲字符输出流。     */    public BufferedWriter(Writer out, int sz) {        super(out);        if (sz <= 0)            throw new IllegalArgumentException("Buffer size <= 0");        this.out = out;        cb = new char[sz];        nChars = sz;        nextChar = 0;        //???        lineSeparator = java.security.AccessController.doPrivileged(            new sun.security.action.GetPropertyAction("line.separator"));    }    /** 确认输出流处于打开状态。如果底层输出流不为null,则认为流处于打开状态 */    private void ensureOpen() throws IOException {        if (out == null)            throw new IOException("Stream closed");    }    /**     * 刷新输出缓冲区,将缓冲区数据写入底层输出流中。     */    void flushBuffer() throws IOException {        synchronized (lock) {            ensureOpen();            if (nextChar == 0)                return;            //将将缓冲区数据写入底层输出流中            out.write(cb, 0, nextChar);            //清空缓冲区            nextChar = 0;        }    }    /**     * 向缓冲区中写入单个字符     */    public void write(int c) throws IOException {        synchronized (lock) {            ensureOpen();            //如果缓冲区已满,刷新缓冲区            if (nextChar >= nChars)                flushBuffer();            //向缓冲区中写入单个字符            cb[nextChar++] = (char) c;        }    }    /**     * 取最小值     */    private int min(int a, int b) {        if (a < b) return a;        return b;    }    /**     * 写入字符数组的某一部分。     * 将字符数组的cbuf中从下标off开始的len个字符写入缓冲区cb中     */    public void write(char cbuf[], int off, int len) throws IOException {        synchronized (lock) {            ensureOpen();            //检查参数是否合法            if ((off < 0) || (off > cbuf.length) || (len < 0) ||                ((off + len) > cbuf.length) || ((off + len) < 0)) {                throw new IndexOutOfBoundsException();            } else if (len == 0) {                return;            }            //如果cbuf的数据的长度大于等于缓冲区长度            if (len >= nChars) {                //刷新缓冲区,将要写入的数据直接写入到底层输出流中                flushBuffer();                out.write(cbuf, off, len);                return;            }            //如果cbuf的数据的长度小于缓冲区长度,将数据写入缓冲区中。            int b = off, t = off + len;            while (b < t) {                int d = min(nChars - nextChar, t - b);                System.arraycopy(cbuf, b, cb, nextChar, d);                b += d;                nextChar += d;                //如果写入过程中缓冲区满了,刷新缓冲区,继续将数据写入缓冲区。                if (nextChar >= nChars)                    flushBuffer();            }        }    }    /**     * 写入字符串的某一部分。     * 将字符串s中从下标off开始的len个字符写入缓冲区cb中     */    public void write(String s, int off, int len) throws IOException {        synchronized (lock) {            ensureOpen();            //???不需要判断参数是否合法            //将字符串s中从下标off开始的len个字符写入缓冲区cb中            int b = off, t = off + len;            while (b < t) {                int d = min(nChars - nextChar, t - b);                s.getChars(b, b + d, cb, nextChar);                b += d;                nextChar += d;                //如果写入过程中缓冲区满了,刷新缓冲区,继续将数据写入缓冲区。                if (nextChar >= nChars)                    flushBuffer();            }        }    }    /**     * 写入一个行分隔符。     * 行分隔符字符串由系统属性line.separator定义,并且不一定是单个新行('\n')符。     */    public void newLine() throws IOException {        write(lineSeparator);    }    /**     * 刷新该流的缓冲。     */    public void flush() throws IOException {        synchronized (lock) {            flushBuffer();            out.flush();        }    }    //关闭此流。关闭前先刷新它。    @SuppressWarnings("try")    public void close() throws IOException {        synchronized (lock) {            if (out == null) {                return;            }            try (Writer w = out) {                flushBuffer();            } finally {                out = null;                cb = null;            }        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
demoimport java.io.BufferedWriter;import java.io.File;import java.io.FileWriter;import java.io.IOException;import org.junit.Test;/** * BufferedWriter Demo */public class BufferedWriterTest {    @Test    private static void test() {        try {            File file = new File("bufferedWriter.txt");            BufferedWriter writer = new BufferedWriter(new FileWriter(file));            writer.write(new char[] { 'a', 'b', 'c', 'd', 'e' }, 0, 3);            writer.newLine();            writer.write("ABCDEFGHIJKLMN", 0, 3);            writer.newLine();            writer.write('\n');            writer.write(1200);            writer.flush();            writer.close();        } catch (IOException e) {            e.printStackTrace();        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

执行测试方法后,bufferedWriter.txt内容为

abcABC?
  • 1
  • 2
  • 3
  • 4
总结
  • BufferedReader,字符缓冲输入流,作用是为其他输入流提供缓冲功能。BufferedReader从其他字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
  • BufferedReader支持标记功能。
  • BufferedWriter,字符缓冲输出流,作用是为其他输出流提供缓冲功能。BufferedWriter将文本写入其他字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。
  • BufferedReader与BufferedWriter是如何提供装饰功能的请参考Java8 I/O源码-FilterInputStream、FilterOutputStream与装饰模式设计模式(9)-装饰模式

关于BufferedReader与BufferedWriter就讲到这里,想了解更多内容请参考

版权声明


1 个回复

倒序浏览

很不错,受教了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马