DataInputStream class readInt () método : Aquí, vamos a aprender sobre el método readInt () de DataInputStream Class con su sintaxis y su ejemplo.
DataInputStream Class readInt () método
- método
- readInt () está disponible en java.io Class.
- readInt () método se utiliza para leer 4 bytes (es decir, 32 bits) del valor class de entrada de datos y devuelve un valor entero leidos.
- readInt () método es un método no package, se puede acceder con sólo el objeto int y si tratamos de acceder al método con el nombre static entonces obtendrá un error.
- método
readInt () puede class una excepción en el momento de la lectura de class.- IOException : Esta excepción puede throw cuando no se abre esta corriente.
- EndOfFileException : Esta excepción puede int cuando esta corriente ha llegado a su punto final.
Sintaxis:
public final void readInt();
Parámetro (s):
- No acepta cualquier parámetro.
throw valor:
El tipo throw del método es Return , no devuelve nada.
Ejemplo:
// Java program to demonstrate the example
// of void readInt() method of
// DataInputStream
import java.io.*;
public class ReadIntOfDIS {
public static void main(String[] args) throws IOException {
InputStream is_stm = null;
DataInputStream dis_stm = null;
FileOutputStream fos_stm = null;
DataOutputStream dos_stm = null;
int[] i_arr = {
100,
200,
300,
400,
500
};
try {
// Instantiate FileInputStream,
// DataInputStream, FileOutputStream
// and DataOutputStream
fos_stm = new FileOutputStream("C:UsersPreeti JainDesktopprogramsincludehelp.txt");
dos_stm = new DataOutputStream(fos_stm);
// Loop to write each int till end
for (int val: i_arr) {
// By using writeInt() method isto
// write an integer to the
// DataOutputStream dos_stm
dos_stm.writeInt(val);
}
is_stm = new FileInputStream("C:UsersPreeti JainDesktopprogramsincludehelp.txt");
dis_stm = new DataInputStream(is_stm);
// Loop To Read Available Data till end
while (dis_stm.available() > 0) {
// By using readInt() method isto read
// integer at a time from dis_stm
int in = dis_stm.readInt();
System.out.println("dis_stm.readInt(): " + in );
}
} catch (Exception ex) {
System.out.println(ex.toString());
} finally {
// To free system resources linked
// with these streams
if (is_stm != null)
is_stm.close();
if (dis_stm != null)
dis_stm.close();
if (dos_stm != null)
dos_stm.close();
if (fos_stm != null)
fos_stm.close();
}
}
}
salida
dis_stm.readInt(): 100
dis_stm.readInt(): 200
dis_stm.readInt(): 300
dis_stm.readInt(): 400
dis_stm.readInt(): 500