Java Java.io.BufferedWriter.write() 方法

  • 描述

    java.io.BufferedWriter.write(char[] cbuf, int off, int len)方法将一段字符缓冲区写入编写器。偏移量,从该偏移量开始读取字符,而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.jc2182;
    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