import java.io.*; public class FileReadExample { public static void main(String[] args) { try { RandomAccessFile file = new RandomAccessFile("data.bin", "r"); long value = file.readLong(); System.out.println("Read value: " + value); file.close(); } catch (IOException e) { e.printStackTrace(); } } }
import java.io.*; public class FileReadExample { public static void main(String[] args) { try { RandomAccessFile file = new RandomAccessFile("data.bin", "r"); for (int i = 0; i < 10; i++) { long value = file.readLong(); System.out.println("Read value " + i + ": " + value); } file.close(); } catch (IOException e) { e.printStackTrace(); } } }This code opens the same binary file as the previous example and reads 10 long values from it using a for loop. It prints the values to the console as it reads them and then closes the file. Both examples use the RandomAccessFile class and the readLong() method to read long values from a binary file.