Home Tutorial Java Corejava Zip How to write content of one file to another file and calculate checksum for accuracy.

 
 

Share on Google+Share on Google+
How to write content of one file to another file and calculate checksum for accuracy.
Posted on: July 12, 2010 at 12:00 AM
Advertisement
This tutorial demonstrate how to write content of one file to another file and also check for accuracy.

CheckSum Calculation in java

This example calculate the checksum by using  CheckedInputStream and CheckedOutputStream classes. It checks the accuracy and integrity of data using checksum. The CheckedInputStream  class  compute checksum value as data read from associated stream. The CheckedOutputStream class is used to compute the checksum of the data begin written on the output stream. 

In this Example, we will write the content of a file to another file. And calculate the value of checksum at writing time with the help of CheckedOutputStream class. After it we will read the content of new file and calculate the value of checksum at reading time with the help of CheckedInputStream  class. If both value of  checksum is equal, it means  data is accurate otherwise there is an error in data..

About CheckedInputStream class:

The java.util.zip.CheckedInputStream class extends java.io.FilterIntputStream class. It provides the following methods:

Return type Method Description
Checksum getChecksum()  Function returns the checksum of the associated steam.
void read(byte b) Method read one byte of data from the stream
void read(byte[] b, int off, int len) The read(..) method read an array of bytes onto the associated stream.
void skip(int n) Method skip specified number of  byte of data from the input stream

About CheckedOutputStream class:

The java.util.zip.CheckedOutputStream class extends java.io.FilterOutputStream class. It provides the following methods:

Return type Method Description
Checksum getChecksum()  Function returns the checksum of the associated steam.
void write(byte[] b, int off, int len) The write(..) method writes an array of bytes onto the associated stream.
void write(int b) Method writes one byte of data onto the stream

 Code:

import java.io.*;
import java.io.IOException;
import java.util.zip.*;
import java.util.zip.CheckedOutputStream;
import java.util.zip.CheckedInputStream;

public class CheckedDemo {
  static long readCis, writeCis;

  public static void main(String args[]) {
    String newfile = "newtestfile.txt";
    String oldfile = "oldtestfile1.txt";
    byte[] buffer = new byte[1024];
    try {
      FileOutputStream fout = new FileOutputStream(newfile);
      CheckedOutputStream outputtream = new CheckedOutputStream(fout,
          new CRC32());
      FileInputStream inStream = new FileInputStream(oldfile);
      int length;
      while ((length = inStream.read(buffer)) 0) {
        outputtream.write(buffer, 0, length);
      }
      inStream.close();
      long writeCis = outputtream.getChecksum().getValue();
      System.out.println("The value of Checksum is at writing time : "
          + writeCis);

    catch (IOException ex) {

      System.out.println("Exception : " + ex.getMessage());
    }
    try {
      FileInputStream fin = new FileInputStream(oldfile);
      CheckedInputStream inputStream = new CheckedInputStream(fin,
          new CRC32());
      byte[] buffer2 = new byte[100];
      while (inputStream.read(buffer2>= 0) {
        long readCis = inputStream.getChecksum().getValue();
        System.out
        .println("value of checksum After reading  new file : "
            + readCis);
      }
    catch (IOException e) {
      System.out.println("the exception has been thrown" + e);
      System.exit(1);
    }
    if (writeCis == readCis)

    {
      System.out.println("No  error in new file data");
    else {

      System.out.println(" error in new file data");
    }
  }

}

Following is the output if you run the application:

 C:\>java CheckedDemo
 The value of Checksum is at writing time : 1792016811
 value of checksum Affter reading new file : 1792016811 
 No error in new file data

Download Source Code

Advertisement

Related Tags for How to write content of one file to another file and calculate checksum for accuracy.:


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 12, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com