请完成下列Java程序:读取自己的源文件并把它压缩成GZIP文件。 注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。 import java.io.*; import java.util.zip.*; public class exl6 2 { public static void main{String[] arg) { ex16_2 obj16_2 = new ex16_2(); obj16_2.doZip("ex16_2.java","ex16_2.gzip") } public void doZip(String strIn,String strOut FileInputStream in; FileOutputStream out; GZIPOutputStream gzip; int nFileLength; byte[] barray = new byte[10]; try {in = new FileInputStream(strIn);out = new FileOutputStream(strOut);gzip = new GZIPOutputStream(out);while((nFileLength = in.read(barray, 0 barray.length)) > 0) _____________________;______________________;gzip.close(); } catch(Exception e) {e.printStackTrace(); } } }
参考答案:
解析:gzip.write(barray,0,nFileLength) in.close() 本题主要考查文件的读写操作。解题关键是熟悉文件读写操作,熟悉GZIP的编程模式,使用FileInputStream读取待压缩的文件,使用GZIPOutputStream进行压缩,除了压缩之外还有解压缩,但是编程的模式一样,一旦压缩数据流被打开,可以象其他数据流一样进行读写。本题中,第1个空,熟练掌握压缩数据流的写操作;第2今空,关闭输出文件刷新输出缓冲器很重要,因为程序没有执行正常刷新数据的垃圾回收,所以就会暂停程序的执行。