Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java read binary file

farenda 2016-02-17 0

Problem:

How to read binary file in Java? The simplest solution is to use FileInputStream with try-with-resources as this in this example.

In the last post we’ve shown how to write binary file. In this post we’re going to show how to read it back into our Java program.

Solution:

The solution is simple, we’re going to use java.io.FileInputStream, but because our binary data is a string we’ll add one twist to the code. We are going to use java.io.Reader to read it. To do that we’ll pass the InputStream into InputStreamReader to turn it into the Reader and read as any other string! If we wouldn’t do that, then we would need to read byte array and then turn it into a String.

Let’s see the code:

package com.farenda.java.io;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class ReadBinaryFileExample {

    public static void main(String[] args) throws IOException {
        String dataFile = "binary-file.dat";

        StringBuilder sb = new StringBuilder();
        try (BufferedReader input = openFile(dataFile)) {
            String line;
            while ((line = input.readLine()) != null) {
                sb.append(line);
            }
        }

        System.out.println("Data from file:\n" + sb);
    }

    private static BufferedReader openFile(String fileName)
            throws IOException {
        // Don't forget to add buffering to have better performance!
        return new BufferedReader(
                new InputStreamReader(
                        new FileInputStream(fileName),
                        StandardCharsets.UTF_8));
    }
}

Now, let’s say that we’ve got binary file with the following content:

$> cat binary-file.dat
Saluton, mondo!Some more data!

If we run the above program, the output is this:

Data from file:
Saluton, mondo!Some more data!
Share with the World!
Categories Java Tags java, java-io
Previous: Java write binary file
Next: Java RandomAccessFile in action

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2021

sponsored