使用SequenceInputStream类
在软件的日常运行中,会生成一些日志文件,记录软件的使用情况,这些文件通常几天生成一个,为方便管理,需要定期合并,可以使用SequenceInputStream实现。SequenceInputStream有一组有序输入流集合创建,并从头至尾逐个读取,可以将所有输入流作为一个整体写入到文件中。可以佳话输出流相关代码,其缺点在于没有充分利用文本文件的特性
package com.utils;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class FileconcatenationDemo {
public static void main(String[] args) throws Exception {
// TODO 自动生成的方法存根
File files[] = new File("D://tmp").listFiles();
List<FileInputStream> FISList = new ArrayList<FileInputStream>();
for (File file : files) {
try {
FISList.add(new FileInputStream(file));
} catch (FileNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
SequenceInputStream sis = new SequenceInputStream(
Collections.enumeration(FISList));
BufferedInputStream bis = new BufferedInputStream(sis);
FileOutputStream fos = null;
fos = new FileOutputStream("d://mr.txt");
int flag = 0;
while ((flag = bis.read()) != -1) {
fos.write(flag);
}
}
}
将 tmp 文件夹下所有文件写到 rm.txt文件中
posted on 2016-02-27 20:19 1130136248 阅读(193) 评论(0) 收藏 举报
浙公网安备 33010602011771号