Read bytes and display their hexadecimal values in Java

Description

The following code shows how to read bytes and display their hexadecimal values.

Example


//w w w . j a va2  s. c o  m
import java.io.FileInputStream;

public class Main {
  public static void main(String[] argv) throws Exception {
    FileInputStream fin = new FileInputStream("text.txt");

    int len;
    byte data[] = new byte[16];

    // Read bytes until EOF is encountered.
    do {
      len = fin.read(data);
      for (int j = 0; j < len; j++)
        System.out.printf("%02X ", data[j]);
    } while (len != -1);
  }
}




















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