Método PrintWriter write(String, int, int) en Java con ejemplos

El método write(String, int, int) de PrintWriter Class en Java se usa para escribir una parte específica de la string especificada en la transmisión. Este String se toma como parámetro. El índice inicial y la longitud del String a escribir también se toman como parámetros.

Sintaxis:

public void write(String string, int índiceInicial, int longitudDestring)

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

  • string que es la string que se escribirá en el flujo.
  • índiceInicial que es el índice inicial desde el cual se tomará la parte del carácter.
  • lengthOfstring, que es la longitud de String que se escribirá en la secuencia.

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

Los siguientes métodos ilustran el funcionamiento del método write(String, int, int):

Programa 1:

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

Geeks

Programa 2:

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

G

Publicación traducida automáticamente

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