java.io.BufferedWriter.write(char[] cbuf, int off, int len) 方法寫入的字元緩衝區一塊到writer。抵消了從開始讀取字元,len是從字元緩衝區的部分的長度。
以下是宣告java.io.BufferedWriter.write(char[] cbuf, int off, int len) 方法:
public void write(char[] cbuf, int off, int len)
cbuf -- 要寫入的字元陣列
off -- 關閉偏移開始讀取字元緩衝區
len -- 被寫入到該流的字元數
此方法不返回任何值。
IOException -- 如果發生I/ O錯誤。
下面的例子顯示java.io.BufferedWriter.write(char[] cbuf, int off, int len) 方法。
package com.yiibai; import java.io.BufferedWriter; import java.io.IOException; import java.io.StringWriter; public class BufferedWriterDemo { public static void main(String[] args) throws IOException { StringWriter sw = null; BufferedWriter bw = null; char[] cbuf = "ABCDEFGHIJKLMN".toCharArray(); try{ // create string writer sw = new StringWriter(); //create buffered writer bw = new BufferedWriter(sw); // write from specified character buffer to stream bw.write(cbuf, 2, 5); // forces out the characters to string writer bw.flush(); // string buffer is created StringBuffer sb = sw.getBuffer(); //prints the string System.out.println(sb); }catch(IOException e){ // if I/O error occurs e.printStackTrace(); }finally{ // releases any system resources associated with the stream if(sw!=null) sw.close(); if(bw!=null) bw.close(); } } }
讓我們來編譯和執行上面的程式,這將產生以下結果:
CDEFG