Display file contents in hexadecimal in Java

Description

The following code shows how to display file contents in hexadecimal.

Example


/* w ww  . jav  a  2  s .c om*/
import java.io.FileInputStream;

public class Main {
  public static void main(String[] args) throws Exception {
    FileInputStream fis = new FileInputStream("main.java");
    int i = 0;
    int count = 0;

    while ((i = fis.read()) != -1) {
      if (i != -1) {
        System.out.printf("%02X ", i);
        count++;
      }

      if (count == 16) {
        System.out.println("");
        count = 0;
      }
    }
    fis.close();
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    I/O »




Binary File
Byte Array
CharSet
Checksum
Console
Create Copy Move Delete
Directory
Drive
Encode Decode
File Attribute
File Lock
File System
GZIP
Jar File
NIO Buffer
Path
Scanner
StreamTokenizer
Temporary File
Text File
Zip