Practice Exercises Java - Learn to program performing exercises with Java

Practice Exercises Java

Learn to program performing exercises with Java

12 Lessons Java with the Solutions - 228 Exercises Java with the solutions
For Beginners, Intermediates and Advanceds

App Practice Exercises Java

The human knowledge belongs to the world
¡The infomation should be free!

Dump - Practice Exercises Java

Lesson 8:

File management

Exercise 8.36:



Create a "dump" utility: an hex viewer, to display the contents of a file, 16 bytes in each row, 24 files in each screen (and then it must pause before displaying the next 24 rows).

In each row, the 16 bytes must be displayed first in Hex and then as characters (the bytes under 32 must be displayed as a dot, instead of the corresponding non printable character).

Look for "hex editor" in Google Images, if you want to see and example of the expected appearance.

Source Code:

import java.util.*;
public class Main
	public static void main(String[] args)
	{ file;
		final int SIZE_BUFFER = 16;

		String name = new Scanner(;

			file = File.OpenRead(name);
			byte[] data = new byte[SIZE_BUFFER];

			int amount;
			int c = 0;

			String line;
				System.out.print(ToHex(file.Position, 8));
				System.out.print("  ");

				amount =, 0, SIZE_BUFFER);

				for (int i = 0; i < amount; i++)
					System.out.print(ToHex(data[i], 2) + " ");

					if (data[i] < 32)
						line += ".";
						line += (char)data[i];

				if (amount < SIZE_BUFFER)
					for (int i = amount; i < SIZE_BUFFER; i++)
						System.out.print("   ");

				line = "";

				if (c == 24)
					new Scanner(;
					c = 0;
			} while (amount == SIZE_BUFFER);

		catch (RuntimeException e)

	public static String ToHex(int n, int digits)
		String hex = String.valueOf(n, 16);
		while (hex.length() < digits)
			hex = "0" + hex;
		return hex;
Exercisey 8.36

Privacy Policy:

Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.


This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.