请完成下列Java程序:读取自己的源文件并把它压缩成GZIP文件。 注意:请勿改动main()主方法和其他已
请完成下列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(barray0nFileLength) in.close()gzip.write(barray,0,nFileLength) in.close() 解析:本题主要考查文件的读写操作。解题关键是熟悉文件读写操作,熟悉GZIP的编程模式,使用FileInputStream读取待压缩的文件,使用GZIPOutputStream进行压缩,除了压缩之外还有解压缩,但是编程的模式一样,一旦压缩数据流被打开,可以象其他数据流一样进行读写。本题中,第1个空,熟练掌握压缩数据流的写操作;第2今
词条内容仅供参考,如果您需要解决具体问题
(尤其在法律、医学等领域),建议您咨询相关领域专业人士。
