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



Description

The java.util.zip.CheckedOutputStream.write(byte[] b, int off, int len) method writes an array of bytes. Will block until the bytes are actually written.

Declaration

Following is the declaration for java.util.zip.CheckedOutputStream.write(byte[] b, int off, int len) method.

public void write(byte[] b, int off, int len)
   throws IOException

Parameters

  • b − the buffer into which the data to be written.

  • off − the start offset in the destination array b.

  • len − the number of bytes 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(byte[] b, int off, int len) 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 length;
         while((length = fin.read(buffer)) > 0) {
            checksum.write(buffer, 0, length);
         }
         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