Java中的Java.io.CharArrayWrite类| 1

作者: Arvin Chen 分类: Java 来源: Break易站(www.breakyizhan.com)
  •   Java I/O总结

    Java中的Java.io.CharArrayWrite类| 1

    Java.io.CharArrayWriter类创建一个可以用作编写器的字符缓冲区。数据写入流时缓冲区会自动增长。数据可以使用toCharArray()和toString()来检索。
    语法:

    public class CharArrayWriter
       extends Writer

    构造函数:

    • CharArrayWriter():从指定的字符数组中创建CharArrayWriter。
    • CharArrayWriter(int size):用指定的初始大小创建一个CharArrayWriter。

    方法:

    • write(int char):java.io.CharArrayWriter.write(int char)将单个字符写入Writer。
      句法:

      public void write(int char)
      Parameters : 
      char : int value of the character to be written.
      Return  :
      void
    • write(String str,int offset,int maxlen):java.io.CharArrayWriter.write(String str,int offset,int maxlen)将字符串的一部分写入Writer。
      句法:

      public void write(String str, int offset, int maxlen)
      Parameters : 
      str : string to be written to the Writer.
      offset : start position of the String
      maxlen : maximum length upto which string has to written
      Return  :
      void
    • write(char [] carray,int offset,int maxlen):java.io.CharArrayWriter.write(char [] carray,int offset,int maxlen)将字符数组的一部分写入Writer。
      句法:

      public void write(char[] carray, int offset, int maxlen)
      Parameters : 
      carray : character to be written to the Writer
      offset : start position of the character array
      maxlen : maximum no. of the character of the carray has to written
      Return  :
      void
    • writeTo(Writer out_stream):java.io.CharArrayWriter.writeTo(Writer out_stream)将缓冲区的内容写入另一个指定的流。
      句法 :

      public void writeTo(Writer out_stream)
      Parameters : 
      out_stream : destination stream to be write into
      Return  :
      void
      Exception : 
      IOException : In case of I/O error occurs
    • toString():java.io.CharArrayWriter.toString()以Writer的字符串形式返回缓冲区内容。
      句法:

      public String toString()
      Parameters : 
      -----------
      Return  :
      returns buffer content as a string from the Writer.
    • close():java.io.StringWriter.close()关闭Writer流但不释放缓冲区
      语法:

      public void close()
      Parameters : 
      -----------
      Return  :
      void
    • size():java.io.StringWriter.size()以整数值的形式返回缓冲区的当前大小。
      句法:

      public int size()
      Parameters : 
      -----------
      Return  :
      integer value representing the current size of the buffer.

    使用CharArrayWriter类方法的Java代码

    // Java program illustrating the working of CharArrayWriter class methods
    // write(int char), toString(), write(char[] carray, int offset, int maxlen)
    // write(String str, int offset, int maxlen), size()
    
    import java.io.*;
    public class NewClass
    {
    	public static void main(String[] args) throws IOException
    	{
    
    		// Initailizing the character array
    		char[] geek = {'G', 'E', 'E', 'K', 'S'};
    		String geek_str;
    
    		// Initailizing the CharArrayWriter
    		CharArrayWriter char_array1 = new CharArrayWriter();
    		CharArrayWriter char_array2 = new CharArrayWriter();
    		CharArrayWriter char_array3 = new CharArrayWriter();
    
    		for(int c = 72; c < 77; c++)
    		{
    			// Use of write(int char)
    			// Writer int value to the Writer
    			char_array1.write(c);
    		}
    
    		// Use of toString() : returning Buffer content as String
    		geek_str = char_array1.toString();
    		System.out.println("Using write(int char) : "+ geek_str);
    
    
    		// Use of write(String str, int offset, int maxlen)
    		// writes some part of the string to the Writer.
    		char_array2.write(geek_str, 2, 3);
    
    		System.out.println("write(str, offset, maxlen) : "+ char_array2.toString());
    
    
    		// Use of write(char[] carray, int offset, int maxlen)
    		// writes some part of the Char[] geek to the Writer
    		char_array3.write(geek, 2, 3);
    		System.out.println("write(carray, offset, maxlen) : "+ char_array3.toString());
    
    		// get buffered content as string
    		String str = char_array3.toString();
    
    
    		// Use of writeTo(Writer out_stream)
    		char_array3.writeTo(char_array1);
    
    		System.out.println("\nchar_array3 to char_array1 : "+ char_array1.toString());
    
    
    		// Use of size() method
    		System.out.println("\nSize of char_array1 : "+ char_array1.size());
    		System.out.println("Size of char_array1 : "+ char_array2.size());
    		System.out.println("Size of char_array1 : "+ char_array3.size());
    
    	}
    }
    

    输出:

    Using write(int char) : HIJKL
    write(str, offset, maxlen) : JKL
    write(carray, offset, maxlen) : EKS
    
    char_array3 to char_array1 : HIJKLEKS
    
    Size of char_array1 : 8
    Size of char_array1 : 3
    Size of char_array1 : 3
  •   Java I/O总结
  •   本文标题:Java中的Java.io.CharArrayWrite类| 1 - Break易站
    转载请保留页面地址:https://www.breakyizhan.com/java/4286.html

    发表笔记

    电子邮件地址不会被公开。 必填项已用*标注