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

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

Exercises Java with Examples-
Practice Exercises Java 4,5 5 302940
Google Ads

Display BMP on console - Practice Exercises Java

Lesson 8:

File management

Exercise 8.41:

Display BMP on console


Create a program to display a 72x24 BMP file on console.
You must use the information on the header (see the exercise of Feb. 7th). Pay attention to the field named "start of image data". After that position, you will find the pixels of the image (you can ignore the information about the color palette and draw a "X" when color is 255, and a blank space if the color is a different one).

Note: you can create a test image, with the following steps (on Paint for Windows): Open Paint, create a new image, change its properties in File menu so that it is a color image, width 72, height 24, save as "256 color bitmap (BMP)".

Source Code:

import java.util.*;
public class Main
	public static void main(String[] args)
		BinaryReader inputFile;

		String fileName = "";

		if (args.length != 1)
			System.out.println("Please enter filename...");
			fileName = new Scanner(;
			fileName = args[0];

		if (!(new
			System.out.println("the file not exists");

			inputFile = new BinaryReader(File.Open(fileName, FileMode.Open));

			// Leo cabecera 1: deber ser P5
			char tag1 = (char)inputFile.ReadByte();
			char tag2 = (char)inputFile.ReadByte();
			byte endOfLine = inputFile.ReadByte();
			if ((tag1 != 'P') || (tag2 != '5') || (endOfLine != 10))
				System.out.println("The file is not a PGM");

			// Leo cabecera 2: ancho y alto
			byte data = 0;
			String sizeOfImage = "";
			while (data != 10)
				data = inputFile.ReadByte();
				sizeOfImage += (char)data;
			String[] widthAndHeight = sizeOfImage.split("[ ]", -1);
			int width = Integer.parseInt(widthAndHeight[0]);
			int height = Integer.parseInt(widthAndHeight[1]);

			// Leo cabecera 3: deber ser 255
			char maxIntensity1 = (char)inputFile.ReadByte();
			char maxIntensity2 = (char)inputFile.ReadByte();
			char maxIntensity3 = (char)inputFile.ReadByte();
			byte endOfLine1 = inputFile.ReadByte();
			if ((maxIntensity1 != '2') || (maxIntensity2 != '5') || (maxIntensity3 != '5') || (endOfLine1 != 10))
				System.out.println("Must be 256 grey levels");
		catch (RuntimeException e)
			System.out.printf("Error: %1$s" + "\r\n", e);
Exercisey 8.41

Share Now:

Google Ads

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.