Home Tutorial Java Core Files Java display file content in hexadecimal format

 
 

Share on Google+Share on Google+
Java display file content in hexadecimal format
Posted on: June 5, 2006 at 12:00 AM
Advertisement
In this section, you will learn how to read the file content and display it in hexadecimal format.

Java display file content in hexadecimal format

In this section, you will learn how to read the file content and display it in hexadecimal format.

Reading a file is a common task but here we are going to display the file data in hexadecimal format. For this we have used FileInputStream class to open a file and initialized a variable to hold a single byte of the file data. Then we have read the file contents byte by byte and print the value in hexadecimal format. If you want to process the file faster then you may read the file contents into some array of bytes.

Here is the code:

import java.io.*;

public class FileInHexadecimal {
	public static void main(String[] args) throws Exception {
		FileInputStream fis = new FileInputStream("C:/data.txt");
		int i = 0;
		while ((i = fis.read()) != -1) {
			if (i != -1) {
				System.out.printf("%02X ", i);
			}
		}
		fis.close();
	}
}
Advertisement

Related Tags for Java display file content in hexadecimal format:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 5, 2006

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com