📜  Java Java类设置 1

📅  最后修改于: 2022-05-13 01:55:45.016000             🧑  作者: Mango

Java Java类设置 1

Java 中的 CharArrayWriter 类 - 设置 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
// 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("\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