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

El método write(int) de PrintWriter Class en Java se usa para escribir el carácter especificado en la secuencia. Este carácter se especifica usando el valor ASCII del carácter pasado como un valor entero. Este valor entero se toma como parámetro.

Sintaxis:

public void write(int ascii)

Parámetros: este método acepta un parámetro obligatorio ascii que es el valor ASCII del carácter 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(int):

Programa 1:

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

0

Programa 2:

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

A

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 *