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
Advertisement
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

Advertisement

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
 

 

 

DMCA.com