Java 中的 BufferedWriter 介紹_動力節(jié)點Java學院整理
BufferedWriter 介紹
BufferedWriter 是緩沖字符輸出流。它繼承于Writer。
BufferedWriter 的作用是為其他字符輸出流添加一些緩沖功能。
BufferedWriter 函數列表
// 構造函數 BufferedWriter(Writer out) BufferedWriter(Writer out, int sz) void close() // 關閉此流,但要先刷新它。 void flush() // 刷新該流的緩沖。 void newLine() // 寫入一個行分隔符。 void write(char[] cbuf, int off, int len) // 寫入字符數組的某一部分。 void write(int c) // 寫入單個字符。 void write(String s, int off, int len) // 寫入字符串的某一部分。
BufferedWriter 源碼分析(基于jdk1.7.40)
package java.io;
public class BufferedWriter extends Writer {
// 輸出流對象
private Writer out;
// 保存“緩沖輸出流”數據的字符數組
private char cb[];
// nChars 是cb緩沖區(qū)中字符的總的個數
// nextChar 是下一個要讀取的字符在cb緩沖區(qū)中的位置
private int nChars, nextChar;
// 默認字符緩沖區(qū)大小
private static int defaultCharBufferSize = ;
// 行分割符
private String lineSeparator;
// 構造函數,傳入“Writer對象”,默認緩沖區(qū)大小是k
public BufferedWriter(Writer out) {
this(out, defaultCharBufferSize);
}
// 構造函數,傳入“Writer對象”,指定緩沖區(qū)大小是sz
public BufferedWriter(Writer out, int sz) {
super(out);
if (sz <= 0)
throw new IllegalArgumentException("Buffer size <= ");
this.out = out;
cb = new char[sz];
nChars = sz;
nextChar = 0;
lineSeparator = java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction("line.separator"));
}
// 確保“BufferedWriter”是打開狀態(tài)
private void ensureOpen() throws IOException {
if (out == null)
throw new IOException("Stream closed");
}
// 對緩沖區(qū)執(zhí)行flush()操作,將緩沖區(qū)的數據寫入到Writer中
void flushBuffer() throws IOException {
synchronized (lock) {
ensureOpen();
if (nextChar == 0)
return;
out.write(cb, 0, nextChar);
nextChar = 0;
}
}
// 將c寫入到緩沖區(qū)中。先將c轉換為char,然后將其寫入到緩沖區(qū)。
public void write(int c) throws IOException {
synchronized (lock) {
ensureOpen();
// 若緩沖區(qū)滿了,則清空緩沖,將緩沖數據寫入到輸出流中。
if (nextChar >= nChars)
flushBuffer();
cb[nextChar++] = (char) c;
}
}
// 返回a,b中較小的數
private int min(int a, int b) {
if (a < b) return a;
return b;
}
// 將字符數組cbuf寫入到緩沖中,從cbuf的off位置開始寫入,寫入長度是len。
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;
}
if (len >= nChars) {
/* If the request length exceeds the size of the output buffer,
flush the buffer and then write the data directly. In this
way buffered streams will cascade harmlessly. */
flushBuffer();
out.write(cbuf, off, len);
return;
}
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寫入到緩沖中,從s的off位置開始寫入,寫入長度是len。
public void write(String s, int off, int len) throws IOException {
synchronized (lock) {
ensureOpen();
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();
}
}
}
// 將換行符寫入到緩沖中
public void newLine() throws IOException {
write(lineSeparator);
}
// 清空緩沖區(qū)數據
public void flush() throws IOException {
synchronized (lock) {
flushBuffer();
out.flush();
}
}
public void close() throws IOException {
synchronized (lock) {
if (out == null) {
return;
}
try {
flushBuffer();
} finally {
out.close();
out = null;
cb = null;
}
}
}
}說明: BufferedWriter的源碼非常簡單,這里就BufferedWriter的思想進行簡單說明:BufferedWriter通過字符數組來緩沖數據,當緩沖區(qū)滿或者用戶調用flush()函數時,它就會將緩沖區(qū)的數據寫入到輸出流中。
示例代碼
關于BufferedWriter中API的詳細用法,參考示例代碼(BufferedWriterTest.java):
import java.io.BufferedWriter;
import java.io.File;
import java.io.OutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.lang.SecurityException;
import java.util.Scanner;
/**
* BufferedWriter 測試程序
*
*
*/
public class BufferedWriterTest {
private static final int LEN = 5;
// 對應英文字母“abcdefghijklmnopqrstuvwxyz”
//private static final char[] ArrayLetters = "abcdefghijklmnopqrstuvwxyz";
private static final char[] ArrayLetters = new char[] {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
public static void main(String[] args) {
testBufferedWriter() ;
}
/**
* BufferedWriter的API測試函數
*/
private static void testBufferedWriter() {
// 創(chuàng)建“文件輸出流”對應的BufferedWriter
// 它對應緩沖區(qū)的大小是16,即緩沖區(qū)的數據>=16時,會自動將緩沖區(qū)的內容寫入到輸出流。
try {
File file = new File("bufferwriter.txt");
BufferedWriter out =
new BufferedWriter(
new FileWriter(file));
// 將ArrayLetters數組的前10個字符寫入到輸出流中
out.write(ArrayLetters, 0, 10);
// 將“換行符\n”寫入到輸出流中
out.write('\n');
out.flush();
//readUserInput() ;
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 讀取用戶輸入
*/
private static void readUserInput() {
System.out.println("please input a text:");
Scanner reader=new Scanner(System.in);
// 等待一個輸入
String str = reader.next();
System.out.printf("the input is : %s\n", str);
}
}運行結果:
生成文件“bufferwriter.txt”,文件的內容是“abcdefghij”。
以上所述是小編給大家介紹的Java 中的 BufferedWriter知識,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
相關文章
Java?+?Selenium?+?OpenCV解決自動化測試中的滑塊驗證問題
OpenCV是一個基于Apache2.0許可(開源)發(fā)行的跨平臺計算機視覺和機器學習軟件庫,可以運行在Linux、Windows、Android和Mac?OS操作系統(tǒng)上,這篇文章主要介紹了Java?+?Selenium?+?OpenCV解決自動化測試中的滑塊驗證,需要的朋友可以參考下2022-07-07
教你使用idea搭建ssm詳細教程(Spring+Spring Mvc+Mybatis)
今天教大家使用idea搭建ssm詳細教程(Spring+Spring Mvc+Mybatis),文中有非常詳細的圖文介紹及代碼示例,對正在學習使用idea的小伙伴很有幫助,需要的朋友可以參考下2021-05-05
maven打包成第三方jar包且把pom依賴包打入進來的方法
這篇文章主要介紹了maven打包成第三方jar包且把pom依賴包打入進來的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11

