Writer append (CharSequence, int, int) método en Java con ejemplos

El método append(CharSequence, int, int) de Writer Class en Java se usa para agregar una parte específica de la charSequence especificada en el escritor. Esta charSequence se toma como parámetro. También se toman como parámetros el índice inicial y el índice final a escribir.

Sintaxis:

agregar vacío público (CharSequence charSequence, int índice de inicio, int índice final)

Parámetros: Este método acepta tres parámetros obligatorios:

  • charSequence que es la charSequence que se escribirá en el Stream.
  • índiceInicial que es el índice inicial desde el cual se tomará la parte del carácter.
  • endingIndex, que es el índice final que se escribirá en el escritor.

Valor devuelto: este método no devuelve ningún valor.

Los siguientes métodos ilustran el funcionamiento del método append(CharSequence, int, int):

Programa 1:

// Java program to demonstrate
// Writer append(CharSequence, int, int) method
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
  
            // Create a Writer instance
            Writer writer
                = new PrintWriter(System.out);
  
            // Get the charSequence
            // to be written in the writer
            CharSequence charSequence = "GeeksForGeeks";
  
            // Get the starting index
            int startingIndex = 0;
  
            // Get the length of char
            int endingIndex = 5;
  
            // Write the portion of the charSequence
            // to this writer using append() method
            // This will put the charSequence in the writer
            // till it is printed on the console
            writer.append(charSequence,
                          startingIndex,
                          endingIndex);
  
            writer.flush();
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
Producción:

Geeks

Programa 2:

// Java program to demonstrate
// Writer append(CharSequence, int, int) method
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
  
            // Create a Writer instance
            Writer writer
                = new PrintWriter(System.out);
  
            // Get the charSequence
            // to be written in the writer
            CharSequence charSequence = "GFG";
  
            // Get the starting index
            int startingIndex = 2;
  
            // Get the length of char
            int endingIndex = 3;
  
            // Write the portion of the charSequence
            // to this writer using append() method
            // This will put the charSequence in the writer
            // till it is printed on the console
            writer.append(charSequence,
                          startingIndex,
                          endingIndex);
  
            writer.flush();
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
Producción:

G

Publicación traducida automáticamente

Artículo escrito por Code_r 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 *