This tutorial demonstrate how to write content of one file to another file and also check for accuracy.
This tutorial demonstrate how to write content of one file to another file and also check for accuracy.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 |
import java.io.*;
|
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 |