Tôi đang đọc mã của người khác. Đây là ý chính của nó.FileOutputStream vs ByteArrayOutputStream
Một lớp nén và giải nén tệp bằng GZIPInputStream và GZIPOutputStream.
Đây là một đoạn trích về những gì diễn ra trong quá trình nén. inputFile
và outputFile
là các trường hợp của lớp File
.
FileInputStream fis = new FileInputStream(inputFile);
GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream(outputFile));
//the following function copies an input stream to an output stream
IOUtils.copy(fis,gzos);
//outputFile is the compressed file
...
Bây giờ, đây là những gì đang xảy ra trong quá trình giải nén.
GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(inputFile));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//copies input stream to output stream
IOUtils.copy(gzis,baos);
//this method does as its name suggests
FileUtils.writeByteArrayToFile(outputFile, baos.toByteArray());
//outputFile is the decompressed file
...
một lý do có thể lập trình viên ban đầu đã chọn FileOutputStream
trong quá trình nén và giải nén ByteArrayOutputStream
trong là gì? Nó làm tôi bối rối.
Trừ khi có lý do chính đáng, tôi nghĩ rằng tôi đang thay đổi chúng là nhất quán để tránh nhầm lẫn trong tương lai. Đây có phải là một ý tưởng tốt?
IOUtils và FileUtils có phải là propritary hoặc từ lib như commons-io không? – sblundy
@sblundy, chúng là từ libs như commons-io. – Russell