Writer write(String) método en Java con ejemplos

El método write(String) de Writer Class en Java se usa para escribir la string especificada en la secuencia. Este valor de String se toma como parámetro.

Sintaxis:

public void write(String string)

Parámetros: este método acepta una string de parámetros obligatoria que es la string que se escribirá en el flujo.

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

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

Programa 1:

// Java program to demonstrate
// Writer write(String) method
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
  
            // Create a Writer instance
            Writer writer
                = new PrintWriter(System.out);
  
            // Write the String 'GeeksForGeeks'
            // to this writer using write() method
            // This will put the string in the stream
            // till it is printed on the console
            writer.write("GeeksForGeeks");
  
            writer.flush();
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
Producción:

GeeksForGeeks

Programa 2:

// Java program to demonstrate
// Writer write(String) method
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
  
            // Create a Writer instance
            Writer writer
                = new PrintWriter(System.out);
  
            // Write the String 'GFG'
            // to this writer using write() method
            // This will put the string in the stream
            // till it is printed on the console
            writer.write("GFG");
  
            writer.flush();
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
Producción:

GFG

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 *