java.util.zip.CRC32.getValue()
方法返回校驗和值。
宣告
以下是java.util.zip.CRC32.getValue
方法的宣告。
public long getValue()
返回值
當前的校驗和值。
在D:\test>
目錄下用以下內容建立一個檔案Hello.txt。
This is an example.
範例
以下範例顯示了java.util.zip.CRC32.getValue()
方法的用法。
package com.yiibai;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class CRC32Demo {
private static String SOURCE_FILE = "D:\\test\\Hello.txt";
private static String TARGET_FILE = "D:\\test\\Hello.zip";
public static void main(String[] args) {
byte[] buffer = new byte[1024];
try {
FileOutputStream fout = new FileOutputStream(TARGET_FILE);
CheckedOutputStream checksum = new CheckedOutputStream(fout, new CRC32());
ZipOutputStream zout = new ZipOutputStream(checksum);
FileInputStream fin = new FileInputStream(SOURCE_FILE);
zout.putNextEntry(new ZipEntry(SOURCE_FILE));
int length;
while((length = fin.read(buffer)) > 0) {
zout.write(buffer, 0, length);
}
zout.closeEntry();
fin.close();
zout.close();
System.out.println("Zip file generated!");
System.out.println("CRC32 Checksum is : " + checksum.getChecksum().getValue());
} catch(IOException ioe) {
System.out.println("IOException : " + ioe);
}
}
}
執行上面範例程式碼,得到以下結果 -
Zip file generated!
CRC32 Checksum is : 3847524486