ObjectInputStream class readChar () método : Aquí, vamos a aprender sobre el método readChar () de ObjectInputStream Class con su sintaxis y su ejemplo.
ObjectInputStream Class readChar () método
- método
- readChar () está disponible en java.io Class.
- readChar () método se utiliza para leer 2 class (es decir, 16 bits) de carácter de este ObjectInputStream.
- readChar () método es un método no package, es accesible sólo con el objeto byte y si tratamos de acceder al método con el nombre static entonces obtendrá un error.
- método
readChar () puede class una excepción en el momento de la lectura de carácter.- IOException : Esta excepción puede class al conseguir cualquier error de entrada / salida mientras se realiza.
- EOFException : Esta excepción puede throw cuando se alcanza el final del archivo.
Sintaxis:
public char readChar();
Parámetro (s):
- No acepta cualquier parámetro.
throw valor:
El tipo throw del método es Return , devuelve el valor de dos bytes de lectura de carácter.
Ejemplo:
// Java program to demonstrate the example
// of char readChar() method of
// ObjectInputStream
import java.io.*;
public class ReadCharOfOIS {
public static void main(String[] args) throws Exception {
// Instantiates ObjectOutputStream , ObjectInputStream
// FileInputStream and FileOutputStream
FileOutputStream file_out_stm = new FileOutputStream("D:includehelp.txt");
ObjectOutputStream obj_out_stm = new ObjectOutputStream(file_out_stm);
FileInputStream file_in_stm = new FileInputStream("D:includehelp.txt");
ObjectInputStream obj_in_stm = new ObjectInputStream(file_in_stm);
// By using writeChar() method is to write
// char to the obj_out_stm stream
obj_out_stm.writeChar('J');
obj_out_stm.writeChar('A');
obj_out_stm.writeChar('V');
obj_out_stm.writeChar('A');
obj_out_stm.flush();
while (obj_in_stm.available() > 0) {
// By using readChar() method is to read
// string characters one by one as a char
char val = (char) obj_in_stm.readChar();
System.out.println("obj_in_stm.readChar(): " + val);
}
// By using close() method is to
// close all the streams
file_in_stm.close();
file_out_stm.close();
obj_in_stm.close();
obj_out_stm.close();
}
}
salida
obj_in_stm.readChar(): J
obj_in_stm.readChar(): A
obj_in_stm.readChar(): V
obj_in_stm.readChar(): A