Read Binary File Example
This program demonstrates reading binary data from a file.
Problem Statement
Write a Java program that reads binary data from a file.
Source Code
| 1 | import java.io.*; |
| 2 | public class ReadBinaryExample { |
| 3 | public static void main(String[] args) { |
| 4 | System.out.println("Eduinq Read Binary Example"); |
| 5 | try(FileInputStream fis=new FileInputStream("binary.dat")) { |
| 6 | int b; |
| 7 | while((b=fis.read())!=-1){ |
| 8 | System.out.print(b+" "); |
| 9 | } |
| 10 | } catch(IOException e) { |
| 11 | System.out.println("\nEduinq Exception: "+e.getMessage()); |
| 12 | } |
| 13 | } |
| 14 | } |
Program Output
Eduinq Read Binary Example 72 101 108 108 111
Explanation
Binary reading uses FileInputStream. - Reads byte by byte and prints numeric values. - Exception handling prevents crashes if file is missing. - In enterprise systems, binary reading is used for images and serialized data. Conclusion: Shows reading binary files.