Java 中的 BufferedWriter 介紹_動力節(jié)點Java學(xué)院整理
BufferedWriter 介紹
BufferedWriter 是緩沖字符輸出流。它繼承于Writer。
BufferedWriter 的作用是為其他字符輸出流添加一些緩沖功能。
BufferedWriter 函數(shù)列表
// 構(gòu)造函數(shù) BufferedWriter(Writer out) BufferedWriter(Writer out, int sz) void close() // 關(guān)閉此流,但要先刷新它。 void flush() // 刷新該流的緩沖。 void newLine() // 寫入一個行分隔符。 void write(char[] cbuf, int off, int len) // 寫入字符數(shù)組的某一部分。 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; // 保存“緩沖輸出流”數(shù)據(jù)的字符數(shù)組 private char cb[]; // nChars 是cb緩沖區(qū)中字符的總的個數(shù) // nextChar 是下一個要讀取的字符在cb緩沖區(qū)中的位置 private int nChars, nextChar; // 默認字符緩沖區(qū)大小 private static int defaultCharBufferSize = ; // 行分割符 private String lineSeparator; // 構(gòu)造函數(shù),傳入“Writer對象”,默認緩沖區(qū)大小是k public BufferedWriter(Writer out) { this(out, defaultCharBufferSize); } // 構(gòu)造函數(shù),傳入“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ū)的數(shù)據(jù)寫入到Writer中 void flushBuffer() throws IOException { synchronized (lock) { ensureOpen(); if (nextChar == 0) return; out.write(cb, 0, nextChar); nextChar = 0; } } // 將c寫入到緩沖區(qū)中。先將c轉(zhuǎn)換為char,然后將其寫入到緩沖區(qū)。 public void write(int c) throws IOException { synchronized (lock) { ensureOpen(); // 若緩沖區(qū)滿了,則清空緩沖,將緩沖數(shù)據(jù)寫入到輸出流中。 if (nextChar >= nChars) flushBuffer(); cb[nextChar++] = (char) c; } } // 返回a,b中較小的數(shù) private int min(int a, int b) { if (a < b) return a; return b; } // 將字符數(shù)組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ū)數(shù)據(jù) 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通過字符數(shù)組來緩沖數(shù)據(jù),當緩沖區(qū)滿或者用戶調(diào)用flush()函數(shù)時,它就會將緩沖區(qū)的數(shù)據(jù)寫入到輸出流中。
示例代碼
關(guān)于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; // 對應(yīng)英文字母“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測試函數(shù) */ private static void testBufferedWriter() { // 創(chuàng)建“文件輸出流”對應(yīng)的BufferedWriter // 它對應(yīng)緩沖區(qū)的大小是16,即緩沖區(qū)的數(shù)據(jù)>=16時,會自動將緩沖區(qū)的內(nèi)容寫入到輸出流。 try { File file = new File("bufferwriter.txt"); BufferedWriter out = new BufferedWriter( new FileWriter(file)); // 將ArrayLetters數(shù)組的前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); } }
運行結(jié)果:
生成文件“bufferwriter.txt”,文件的內(nèi)容是“abcdefghij”。
以上所述是小編給大家介紹的Java 中的 BufferedWriter知識,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
springmvc實現(xiàn)跨服務(wù)器文件上傳功能
這篇文章主要為大家詳細介紹了springmvc實現(xiàn)跨服務(wù)器文件上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08springboot3生成本地文件url的實現(xiàn)示例
本文主要介紹了springboot3生成本地文件url的實現(xiàn)示例,從而提供一種高效的文件管理方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2024-01-01Java?+?Selenium?+?OpenCV解決自動化測試中的滑塊驗證問題
OpenCV是一個基于Apache2.0許可(開源)發(fā)行的跨平臺計算機視覺和機器學(xué)習軟件庫,可以運行在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),文中有非常詳細的圖文介紹及代碼示例,對正在學(xué)習使用idea的小伙伴很有幫助,需要的朋友可以參考下2021-05-05maven打包成第三方j(luò)ar包且把pom依賴包打入進來的方法
這篇文章主要介紹了maven打包成第三方j(luò)ar包且把pom依賴包打入進來的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11