(20)IO流之SequenceInputStream 序列流

时间:2022-09-07 12:07:15

序列流,对多个流进行合并。

SequenceInputStream 表示其他输入流的逻辑串联。它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。

序列流,对多个流进行合并。

SequenceInputStream 表示其他输入流的逻辑串联。它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。

合并两个流

使用构造函数SequenceInputStream(InputStream s1, InputStream s2)

 //使用SequenceInputStream合并两个文件
public static void merge2() throws IOException{
//找到目标文件
File inFile1 = new File("d:\\a.txt");
File inFile2 = new File("d:\\b.txt");
File outFile = new File("D:\\c.txt");
//建立数据通道
FileOutputStream fileOutputStream = new FileOutputStream(outFile);
FileInputStream fileInputStream1 = new FileInputStream(inFile1);
FileInputStream fileInputStream2 = new FileInputStream(inFile2); //建立序列流对象
SequenceInputStream inputStream = new SequenceInputStream(fileInputStream1,fileInputStream2);
byte[] buf = new byte[];
int length = ;
while((length = inputStream.read(buf))!=-)
{
fileOutputStream.write(buf, , length);
}
inputStream.close();
// fileInputStream1.close(); //查看上面inputStream.close()的源码就可以看到,它穿起来的流一块关闭了
// fileInputStream2.close();
fileOutputStream.close();
}

合并多个文件

 //把三个文件合并成一个文件
public static void merge3() throws IOException
{
//找到目标文件
File file1 = new File("d:\\a.txt");
File file2 = new File("d:\\b.txt");
File file3 = new File("D:\\c.txt");
File file4 = new File("d:\\d.txt");
//建立对应的输入输出流对象
FileInputStream fileInputStream1 = new FileInputStream(file1);
FileInputStream fileInputStream2 = new FileInputStream(file2);
FileInputStream fileInputStream3 = new FileInputStream(file3);
FileOutputStream fileOutputStream = new FileOutputStream(file4); //创建序列流对象
Vector<FileInputStream> vector = new Vector<FileInputStream>();
vector.add(fileInputStream1);
vector.add(fileInputStream2);
vector.add(fileInputStream3);
Enumeration<FileInputStream> e = vector.elements(); SequenceInputStream inputStream = new SequenceInputStream(e); //读取文件的数据
int length =;
byte[] buf = new byte[];
while((length = inputStream.read(buf))!=-)
{
fileOutputStream.write(buf, , length);
}
inputStream.close();
fileOutputStream.close();
}

下面是一个例子把一个mp3文件切割合并的过程:

 public class Demo2
{
public static void main(String[] args) throws IOException
{
cutFile();
mergeFile();
} //合并
public static void mergeFile() throws IOException
{
//找到目标文件
File dir = new File("D:\\part");
Vector<FileInputStream> vector = new Vector<FileInputStream>();
//通过目标文件夹找到所有的mmp3并添加到Vector中
File[] files = dir.listFiles();
for (File file : files)
{
if(file.getName().endsWith("mp3"))
{
vector.add(new FileInputStream(file));
}
} //通过vector获取迭代器对象
Enumeration<FileInputStream> e = vector.elements(); SequenceInputStream inputStream = new SequenceInputStream(e); //建立文件的输出通道
FileOutputStream fileOutputStream = new FileOutputStream("D:\\merge.mp3");
//建立缓冲数组
int length = ;
byte[] buf = new byte[*];
while((length = inputStream.read(buf))!=-)
{
fileOutputStream.write(buf, , length);
}
} //切割
public static void cutFile() throws IOException
{
File file = new File("D:\\1.mp3");
//目标文件夹
File dir = new File("D:\\part");
//建立数据的输入通道
FileInputStream fileInputStream = new FileInputStream(file);
//建立缓存数组存储
byte[] buf = new byte[*];
int length = ;
for(int i = ; (length = fileInputStream.read(buf))!=-; i ++)
{
FileOutputStream fileOutputStream = new FileOutputStream(new File(dir, "part"+i+".mp3"));
fileOutputStream.write(buf, , length);
fileOutputStream.close();
}
//关闭资源
fileInputStream.close();
}
}