Java基礎(chǔ)知識(shí)之CharArrayWriter流的使用
Java CharArrayWriter流
一、CharArrayWriter流定義
API說明:該類實(shí)現(xiàn)了一個(gè)可用作字符輸出流的字符緩沖區(qū),當(dāng)數(shù)據(jù)寫入流時(shí),緩沖區(qū)自動(dòng)增長(zhǎng),請(qǐng)注意在此類上調(diào)用close()無效,并且可以在流關(guān)閉后調(diào)用此類的方法而不生成IOException。
二、CharArrayWriter流構(gòu)造函數(shù)
根據(jù)指定緩沖區(qū)大小或者默認(rèn)緩沖區(qū)大小創(chuàng)建CharArrayWriter流對(duì)象
/** *創(chuàng)造默認(rèn)緩沖區(qū)大小的CharArrayWriter對(duì)象 */ public CharArrayWriter() { this(32); } /** * 創(chuàng)造指定緩沖區(qū)大小的CharArrayWriter對(duì)象 */ public CharArrayWriter(int initialSize) { if (initialSize < 0) { throw new IllegalArgumentException("Negative initial size: " + initialSize); } buf = new char[initialSize]; }
三、CharArrayWriter流實(shí)例域
/** * 字符緩沖區(qū) */ protected char buf[]; /** * 緩沖區(qū)中的當(dāng)前位置 */ protected int count;
四、CharArrayWriter流方法
1)write(int c):寫一個(gè)字符到緩沖區(qū)中
/** * 寫一個(gè)單個(gè)字符到緩沖區(qū)中 */ public void write(int c) { synchronized (lock) { int newcount = count + 1; //判定寫入的下個(gè)元素是否超出緩沖區(qū)長(zhǎng)度 if (newcount > buf.length) { //進(jìn)行擴(kuò)容 buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount)); } buf[count] = (char)c; count = newcount; } }
實(shí)際流程:
2)write(char c[], int off, int len):從字符數(shù)組中寫len個(gè)字符到緩沖區(qū)中
/** * 將字符數(shù)組的一部分寫入到緩沖區(qū)中,自動(dòng)擴(kuò)容增長(zhǎng) */ public void write(char c[], int off, int len) { if ((off < 0) || (off > c.length) || (len < 0) || ((off + len) > c.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return; } synchronized (lock) { int newcount = count + len; //判定緩沖區(qū)寫入len個(gè)字符后長(zhǎng)度是否超出限制 if (newcount > buf.length) { buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount)); } System.arraycopy(c, off, buf, count, len); count = newcount; } }
實(shí)際流程:
3)write(String str, int off, int len):將字符串的一部分寫入到緩沖區(qū)中
public void write(String str, int off, int len) { synchronized (lock) { int newcount = count + len; if (newcount > buf.length) { buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount)); } str.getChars(off, off + len, buf, count); count = newcount; } }
4)writeTo(Writer out):將緩沖區(qū)的內(nèi)容寫入到另一個(gè)字符輸出流中
public void writeTo(Writer out) throws IOException { synchronized (lock) { out.write(buf, 0, count); //本質(zhì)理解為將緩沖區(qū)的內(nèi)容給寫了出去 } }
5)檢索緩沖區(qū)中的數(shù)據(jù)
/** * 將緩沖區(qū)中的數(shù)據(jù)轉(zhuǎn)成字符串 * @return the string. */ public String toString() { synchronized (lock) { return new String(buf, 0, count); } } /** * 將緩沖區(qū)的數(shù)據(jù)轉(zhuǎn)成字符數(shù)組 */ public char toCharArray()[] { synchronized (lock) { return Arrays.copyOf(buf, count); } }
6)close():關(guān)閉流無效,本質(zhì)沒有做任何操作
/** * 刷新流--無效 */ public void flush() { } /** * 關(guān)閉流--無效 */ public void close() { }
四、CharArrayWriter流的作用
與CharArrayReader流一樣,待后期理解加深、實(shí)際項(xiàng)目運(yùn)用過后再來補(bǔ)充
Java基礎(chǔ)之什么是CharArrayWriter
CharArrayWriter 實(shí)現(xiàn)了以數(shù)組作為目標(biāo)的輸出流。CharArrayWriter 有兩個(gè)構(gòu)造函數(shù):
CharArrayWriter( ) CharArrayWriter(int numChars)
第一種形式,創(chuàng)建了一個(gè)默認(rèn)長(zhǎng)度的緩沖器。
第二種形式,緩沖器長(zhǎng)度由numChars指定。
緩沖器保存在CharArrayWriter的buf 成員中。緩沖器大小在需要的情況下可以自動(dòng)增長(zhǎng)。緩沖器保持的字符數(shù)包含在CharArrayWriter的count 成員中。buf 和count 都是受保護(hù)的域。
下面的例子闡述了CharArrayWriter
我們繼續(xù)使用前面顯示的ByteArrayOutputStream 例子中演示的程序。它的輸出與以前的例子輸出相同:
// Demonstrate CharArrayWriter. import java.io.*; class CharArrayWriterDemo { public static void main(String args[]) throws IOException { CharArrayWriter f = new CharArrayWriter(); String s = "This should end up in the array"; char buf[] = new char[s.length()]; s.getChars(0, s.length(), buf, 0); f.write(buf); System.out.println("Buffer as a string"); System.out.println(f.toString()); System.out.println("Into array"); char c[] = f.toCharArray(); for (int i=0; i<c.length; i++) { System.out.print(c[i]); } System.out.println("\nTo a FileWriter()"); FileWriter f2 = new FileWriter("test.txt"); f.writeTo(f2); f2.close(); System.out.println("Doing a reset"); f.reset(); for (int i=0; i<3; i++) f.write('X'); System.out.println(f.toString()); } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- ByteArrayInputStream簡(jiǎn)介和使用_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
- Java使用ByteArrayOutputStream 和 ByteArrayInputStream 避免重復(fù)讀取配置文件的方法
- Java 8 Stream流強(qiáng)大的原理
- Java基礎(chǔ)知識(shí)之ByteArrayOutputStream流的使用
- Java基礎(chǔ)知識(shí)之StringWriter流的使用
- Java?IO流之StringWriter和StringReader用法分析
- Java基礎(chǔ)知識(shí)之ByteArrayInputStream流的使用
相關(guān)文章
springboot2.x只需兩步快速整合log4j2的方法
這篇文章主要介紹了springboot2.x只需兩步快速整合log4j2的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05Spring Boot非Web項(xiàng)目運(yùn)行配置的方法教程
這篇文章主要介紹了Spring Boot非Web項(xiàng)目運(yùn)行配置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09SpringBoot2.動(dòng)態(tài)@Value的實(shí)現(xiàn)方式
這篇文章主要介紹了SpringBoot2.動(dòng)態(tài)@Value的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07SpringBoot自動(dòng)配置深入探究實(shí)現(xiàn)原理
在springboot的啟動(dòng)類中可以看到@SpringBootApplication注解,它是SpringBoot的核心注解,也是一個(gè)組合注解。其中@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan三個(gè)注解尤為重要。今天我們就來淺析這三個(gè)注解的含義2022-08-08java如何將一個(gè)float型數(shù)的整數(shù)部分和小數(shù)分別輸出顯示
這篇文章主要介紹了java如何將一個(gè)float型數(shù)的整數(shù)部分和小數(shù)分別輸出顯示,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07使用工具類-java精確到小數(shù)點(diǎn)后6位
這篇文章主要介紹了使用工具類-java精確到小數(shù)點(diǎn)后6位,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10Java實(shí)現(xiàn)的動(dòng)態(tài)數(shù)字時(shí)鐘功能示例【顯示世界時(shí)間】
這篇文章主要介紹了Java實(shí)現(xiàn)的動(dòng)態(tài)數(shù)字時(shí)鐘功能,結(jié)合實(shí)例形式分析了java顯示北京、紐約、倫敦等世界時(shí)間的相關(guān)日期時(shí)間運(yùn)算操作技巧,需要的朋友可以參考下2019-03-03Spring代理對(duì)象導(dǎo)致的獲取不到原生對(duì)象注解的解決
本文主要介紹了Spring代理對(duì)象導(dǎo)致的獲取不到原生對(duì)象注解的解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04詳解如何使用tldb數(shù)據(jù)庫(kù)的java客戶端
這篇文章主要為大家介紹了如何使用tldb數(shù)據(jù)庫(kù)的java客戶端過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09