场景:将某个文件夹内的txt文件都转为txt.gz文件
1、遍历文件夹内的txt文件
File folder=new File(folderPath)
for (File file : folder.listFiles())
2、用io流转化
输入流
File txtFile=new File(txtFilePath)
FileInputStream fis=new FileInputStream(txtFile);
BufferedInputStream bis=new BufferedInputStream(fis);
输出流
File txtGzFile=new File(txtGzFilePath);
FileOutputStream fos=new FileOutputStream(txtGzFile);
BufferedOutputStream bos=new BufferedOutputStream(fos);
GZIPOutputStream gzos = new GZIPOutputStream(bos);
int len=0;
byte[] buffer=new byte[1024];
while ((len = bis.read(buffer)) != -1) {
gzos.write(buffer, 0, len);
}
gzos.flush();
gzos.close();
bis.close();