Home Tutorial Java Corejava Zip How to read a byte.


Share on Google+Share on Google+
How to read a byte.
Posted on: July 9, 2010 at 12:00 AM
This tutorial demonstrate how to read a byte of data from a file.


In the given Example,  you will see how to use read method of CheckedInputStrem class. It reads single byte of data from the input stream and updates the checksum from that byte data which it read. When data transmit across the network we add CRC (Cyclic Redundancy Check) for  error detecting. CkeckedInputStream use to add checksum with data . The getChecksum  method  returns Checksum  of the stream and the getValue method return the value of checksum.


import java.io.File;
import java.util.zip.CheckedInputStream;
import java.util.zip.CRC32;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

class Demo {
  public static void doChecksum(String fileName) {
    try {

      CheckedInputStream cis = new CheckedInputStream(new FileInputStream(fileName)new CRC32());
      File file = new File(fileName);
      long fileSize = file.length();

      byte[] buf = new byte[128];
      while (cis.read(buf>= 0) {
        long checksum = cis.getChecksum().getValue();
        System.out.println("The Value Of checksum is ::" + checksum);
        System.out.println("The Size Of file Is ::" " " + fileSize);
        System.out.println("Name Of File is ::" " " + fileName);

    catch (IOException e) {


public class CheckReadDemo {
  public static void main(String[] args) {





Download this code


Related Tags for How to read a byte.:

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: July 9, 2010

Recommend the tutorial

Advertisements Advertisements