Home Tutorial Java Corejava Zip Use of write method of CheckedOutputStream class in java.

 
 

Use of write method of CheckedOutputStream class in java.
Posted on: July 19, 2010 at 12:00 AM
Advertisement
In this tutorial you will see the use of write(int b) method of CheckedOutputStream class in java.

Use of write method of CheckedOutputStream class in java.

In this tutorial, we will discuss the use of write(int b) method of CheckedOutputStream class. The CheckedOutputStream class available in java.util.zip package. The CheckedOutputStream class creates a output stream which maintains checksum at writing time and calculate checksum value. The write(int b) method is used to write a bytes on associated output stream. 
In the given Example, we will explain how to use write(int b) method of CheckedOutputStream class. The FileInputStream class create a connection to a file and obtains byte from files. The read() method of FileInputStream class read one byte from input stream. The CheckedOutputStream class creates a output stream which maintains checksum at writing time and calculate checksum value. The getChecksum method returns the checksum of  associated output stream. The getValue() method of Checksum interface returns value of current checksum.   

About CheckedOutputStream API:

Return Type Method Description 
checksum getChecksum() Function getChecksum() returns ckecksum of associated output stream.
void write(int b) The write() method write a byte on associated output stream.

Code

import java.io.*;
import java.util.zip.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.CheckedOutputStream;
import java.io.IOException;

class CheckedWrite {
  public static void main(String args[]) {
    try {
      FileOutputStream fout = new FileOutputStream("testfile1.txt");
      Adler32 adler = new Adler32();
      CheckedOutputStream checkout = new CheckedOutputStream(fout, adler);
      FileInputStream fin = new FileInputStream("testfile.txt");
      int length;
      for (int c = fin.read(); c != -1; c = fin.read()) {
        checkout.write(c);
      }
      System.out
      .println("content of a file successfully written in another file using Write(int b) method.");
      long value = checkout.getChecksum().getValue();
      System.out.println("The Adler32 Checksum value : " + value);
    catch (IOException e) {

      System.out.println("IOException : " + e.getMessage());
    }

  }
}

Following is the output if you run the application:

C:\>javac CheckedWrite.java
C:\Work\Bharat\today\checkedwrite>java CheckedWrite
content of a file successfully written in another file using Write(int b) method.
The Adler32 Checksum value : 507446444

Download this code

Advertisement

Related Tags for Use of write method of CheckedOutputStream class in java.:


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

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com