Home Tutorial Java Corejava Zip How to read a byte.

 
 

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.

Description

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.

Code:

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) {
      e.printStackTrace();
      System.exit(1);
    }

  }
}

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

    Demo.doChecksum(args[0]);

  }

}

Output:

Download this code

Related Tags for How to read a byte.:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.