/****************************************************************************** * This program reads bytes from a file and prints them in hexadecimal format. * * Copyright © 2021 Richard Lesh. All rights reserved. *****************************************************************************/ import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import org.pureprogrammer.Utils; public class FileIO3 { static void readBinaryFile(String filespec) throws IOException { BufferedInputStream ifh = new BufferedInputStream(new FileInputStream(filespec)); int c; int count = 0; while ((c = Utils.getbyte(ifh)) != -1) { System.out.print(Utils.format("{0:02x} ", c)); ++count; if (count % 16 == 0) { System.out.println(); } } if (count % 16 != 0) { System.out.println(); } try {ifh.close();} catch (IOException ex) {}; } public static void main(String[] args) { if (args.length != 1) { System.out.println(Utils.join("", "Syntax: ", "FileIO3", " {filename}")); System.exit(1); } final String FILESPEC = args[0]; try { readBinaryFile(FILESPEC); } catch (IOException ex) { System.out.println(Utils.join("", "Error: ", Utils.exceptionMessage(ex))); } catch (Exception ex) { System.out.println("Unexpected File Read Error"); } } }