java.util.zip.CheckedOutputStream.write() 方法示例



描述

java.util.zip.CheckedOutputStream.write(byte[] b, int off, int len) 方法建立一個位元組陣列。在實際寫入位元組內容之前會持續阻塞。

宣告

以下是 java.util.zip.CheckedOutputStream.write(byte[] b, int off, int len) 方法的宣告。

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

引數

  • b − 要寫入資料的緩衝區。

  • off − 目標陣列 b 中的開始偏移量。

  • len − 要寫入的位元組數。

異常

  • IOException − 如果發生 I/O 錯誤。

預條件

D:> test > 目錄中建立一個名為 Hello.txt 的檔案,內容如下。

This is an example.

示例

以下示例演示瞭如何使用 java.util.zip.CheckedOutputStream.write(byte[] b, int off, int len) 方法。

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);
      }
   }
}

編譯並執行上述程式後,將生成以下結果 −

File copied!
Adler32 Checksum is : 1126631102
javazip_checkedoutputstream.htm
廣告
© . All rights reserved.