import java.io.FileInputStream; import java.io.IOException; public class ReadIntFromFileExample { public static void main(String[] args) { try(FileInputStream fis = new FileInputStream("numbers.bin")) { int num = fis.readInt(); System.out.println(num); } catch(IOException ex) { System.err.println(ex); } } }
import java.io.DataInputStream; import java.io.IOException; import java.net.Socket; public class ReadIntFromSocketExample { public static void main(String[] args) { try(Socket socket = new Socket("localhost", 1234); DataInputStream in = new DataInputStream(socket.getInputStream())) { int num = in.readInt(); System.out.println(num); } catch(IOException ex) { System.err.println(ex); } } }The `readInt` method is part of the `DataInput` interface in the `java.io` package.