java.util.zip.CheckedOutputStream.write() Method Example



Description

The java.util.zip.CheckedOutputStream.write(int b) method writes a byte. Will block until the byte is actually written.

Declaration

Following is the declaration for java.util.zip.CheckedOutputStream.write(int b) method.

public void write(int b)
   throws IOException

Parameters

  • b − the byte to be written.

Exceptions

  • IOException − if an I/O error has occurred.

Pre-requisite

Create a file Hello.txt in D:> test > directory with the following content.

This is an example.

Example

The following example shows the usage of java.util.zip.CheckedOutputStream.write(int b) method.

package com.tutorialspoint;

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

public class CheckedOutputStreamDemo {

   private static String SOURCE_FILE = "D:\\test\\Hello.txt";
   private static String TARGET_FILE = "D:\\test\\Hello1.txt";

   public static void main(String[] args) {
      byte[] buffer = new byte[1024];

      try {
         FileOutputStream fout = new FileOutputStream(TARGET_FILE);
         CheckedOutputStream checksum = new CheckedOutputStream(fout, new Adler32());

         FileInputStream fin = new FileInputStream(SOURCE_FILE);

         int data = 0;
         while((data = fin.read()) != -1) {
            checksum.write(data);
         }
         fin.close();
         fout.close();
         System.out.println("File copied!");
         System.out.println("Adler32 Checksum is : " + checksum.getChecksum().getValue());
      } catch(IOException ioe) {
         System.out.println("IOException : " + ioe);
      }
   }
}

Let us compile and run the above program, this will produce the following result −

File copied!
Adler32 Checksum is : 1126631102
Print
javazip_checkedoutputstream.htm
Advertisements