JAVA伊奥。Java中的CharArrayWriter类|集1

CharArrayWriter class in Java - Set 1

null

JAVA伊奥。CharArrayWriter 类创建可以用作编写器的字符缓冲区。当数据写入流时,缓冲区会自动增长。可以使用ToCharray()和toString()检索数据。 宣言:

public class CharArrayWriter   extends Writer

建造师:

  • CharArrayWriter(): 从指定的字符数组创建CharArrayWriter。
  • CharArrayWriter(整数大小): 正在创建具有指定初始大小的CharArrayWriter。

方法:

  • write(int char):java。伊奥。CharArrayWriter。写入(int char) 给作者写一个字符。 语法:
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。伊奥。CharArrayWriter。写入(字符串str、int offset、int maxlen) 将字符串的某些部分写入编写器。 语法:
public void write(String str, int offset, int maxlen)Parameters : str : string to be written to the Writer.offset : start position of the Stringmaxlen : maximum length upto which string has to writtenReturn  :void
  • 写(char[]carray,int offset,int maxlen):java。伊奥。CharArrayWriter。写入(字符[]carray,整数偏移,整数maxlen) 将字符数组的某些部分写入写入器。 语法:
public void write(char[] carray, int offset, int maxlen)Parameters : carray : character to be written to the Writeroffset : start position of the character arraymaxlen : maximum no. of the character of the carray has to writtenReturn  :void
  • writeTo(Writer out_stream):java。伊奥。CharArrayWriter。writeTo(Writer out_stream) 将缓冲区的内容写入另一个指定流。 语法:
public void writeTo(Writer out_stream)Parameters : out_stream : destination stream to be write intoReturn  :voidException : IOException : In case of I/O error occurs
  • toString():java。伊奥。CharArrayWriter。toString() 以字符串形式从写入程序返回缓冲区内容。 语法:
public String toString()Parameters : -----------Return  :returns buffer content as a string from the Writer.
  • close():java。伊奥。StringWriter。关闭() 关闭写入流,但不释放缓冲区 语法:
public void close()Parameters : -----------Return  :void
  • size():java。伊奥。StringWriter。大小() 以整数值形式返回缓冲区的当前大小。 语法:
public int size()Parameters : -----------Return  :integer value representing the current size of the buffer.

解释CharArrayWriter类方法使用的Java代码

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
{
// Initializing the character array
char [] geek = { 'G' , 'E' , 'E' , 'K' , 'S' };
String geek_str;
// Initializing 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( "char_array3 to char_array1 : " + char_array1.toString());
// Use of size() method
System.out.println( "Size 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) : HIJKLwrite(str, offset, maxlen) : JKLwrite(carray, offset, maxlen) : EKSchar_array3 to char_array1 : HIJKLEKSSize of char_array1 : 8Size of char_array1 : 3Size of char_array1 : 3

下一篇文章: JAVA伊奥。Java | Set2中的CharArrayWriter类 本文由 莫希特·古普塔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享