Clase Java.io.CharArrayWriter en Java | Serie 1

CharArrayWriter class in Java - Set 1

La clase java.io.CharArrayWriter crea un búfer de caracteres que se puede usar como escritor. El búfer crece automáticamente cuando se escriben datos en la secuencia. Los datos se pueden recuperar utilizando toCharArray() y toString(). 
Declaración: 
 

public class CharArrayWriter
   extends Writer

Constructor: 
 

  • CharArrayWriter() : Creación de un CharArrayWriter a partir de una array de caracteres específica.
  • CharArrayWriter(int size) : Crear un CharArrayWriter con el tamaño inicial especificado.

Métodos: 
 

  • write(int char) : java.io.CharArrayWriter.write(int char) escribe un solo carácter en el Escritor. 
    Sintaxis: 
     
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) escribe una parte de la string en el Escritor. 
    Sintaxis: 
     
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) escribe una parte de la array de caracteres en Writer. 
    Sintaxis: 
     
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) escribe el contenido del búfer en otro flujo especificado. 
    Sintaxis: 
     
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() devuelve el contenido del búfer como una string del Escritor. 
    Sintaxis: 
     
public String toString()
Parameters : 
-----------
Return  :
returns buffer content as a string from the Writer.
  •  
  • close() : java.io.StringWriter.close() cierra el flujo del escritor pero no libera el búfer 
    Sintaxis: 
     
public void close()
Parameters : 
-----------
Return  :
void
  •  
  • size() : java.io.StringWriter.size() devuelve el tamaño actual del búfer como un valor entero. 
    Sintaxis: 
     
public int size()
Parameters : 
-----------
Return  :
integer value representing the current size of the buffer.
  •  

Código Java que explica el uso de los métodos de la clase 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
    {
 
        // 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());
 
    }
}

Producción : 
 

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

Artículo siguiente: Clase Java.io.CharArrayWriter en Java | Set2
Este artículo es una contribución de . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *