Binary java

Introduction to Binary I/O in Java

  • Binary I/O: critical for understanding how data is processed in applications.

Key Objectives

  • Understand how I/O is processed in Java.

  • Distinguish between text I/O and binary I/O.

  • Perform byte reading and writing using FileInputStream and FileOutputStream.

  • Use DataInputStream/DataOutputStream for primitive values and strings.

  • Access files randomly using RandomAccessFile.

Understanding I/O Handling in Java

  • File Object: Represents properties of a file/path but not methods to read/write data.

    • Example:

      PrintWriter output = new PrintWriter("temp.txt");  
      output.println("Java 101");  
      output.close();  
      Scanner input = new Scanner(new File("temp.txt"));  
      System.out.println(input.nextLine());  

Text Files vs Binary Files

  • Text files are human-readable while binary files are in binary format (not human-readable).

    • Advantage of Binary Files: More efficient to process.

    • Example: 199 stored as characters "1, 9, 9" in text file, stored as byte C7 in binary file.

Characteristics of Binary I/O

  • Text I/O involves encoding/decoding Unicode to file-specific encoding.

  • Binary I/O copies original bytes directly to/from the file without conversions.

Binary I/O Classes Overview

  • FileInputStream

  • DataInputStream

  • InputStream

  • BufferedInputStream

  • ObjectInputStream

  • FileOutputStream

  • DataOutputStream

  • BufferedOutputStream

  • ObjectOutputStream

InputStream Class Details

  • Abstract Class providing methods to read bytes.

  • Key Methods:

    • read(), read(byte[] b), available(), close(), skip(long n), mark(int readlimit), reset().

    • Behavior: Reads bytes, indicates end of stream with -1.

Writing Data to Binary Files - Example

public class WriteBinaryFileExample {
 public static void main (String[] args) {
 String filePath = "datafile.dat";
 int[] data = {10, 20, 30, 40, 50};
 try (FileOutputStream fos = new FileOutputStream(filePath)) {
 for (int number : data) {
 fos.write(number);
 }
 System.out.println ("Data written to binary file.");
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
}

Reading Data from Binary Files - Example

public class ReadBinaryFileExample {
 public static void main(String[] args) {
 String filePath = "datafile.dat";
 try (FileInputStream fis = new FileInputStream(filePath)) {
 int byteData;
 System.out.println("Data read from binary file:");
 while ((byteData = fis.read()) != -1) {
 System.out.println("Read byte: " + byteData);
 }
 } catch (IOException e) {
 e.printStackTrace();
 }
}
}

Handling Primitive Values with Data Streams

  • DataInputStream: For reading primitive data types in a machine-independent way.

  • DataOutputStream: For writing primitive data types.

Example of Data Streams

public class DataStreamExample {
 public static void main(String[] args) throws IOException { 
 try (DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.bin"))) {
 dos.writeInt(42);
 dos.writeFloat(3.14f);
 dos.writeDouble(2.71828);
 }
 } 
}

Improving I/O Performance with Buffered Streams

  • Using BufferedInputStream and BufferedOutputStream enhances performance via buffering.

  • Allows larger blocks of data to be fetched in memory reducing I/O operations.

Random Access Files

  • RandomAccessFile allows reading/writing at any location in a file.

  • Key Methods: seek(long pos), getFilePointer(), length(), read(), write().

  • Example for access and modification:

RandomAccessFile raf = new RandomAccessFile("test.dat", "rw");

Final Notes

  • Ensure to read and write in the same order and format when using data streams (e.g., UTF-8 with writeUTF).

  • Employ try-with-resources for effective resource management.