本帖最后由 wuming123 于 2015-10-11 15:49 编辑
import java.io.*;
import java.util.*;
class SplitFile
{//练习合并流文件
public static void main(String[] args)
{
FileInputStream file = null;
FileInputStream files = null;
FileInputStream filess = null;
try
{
file = new FileInputStream("E:\\javalianxi\\1.mp3");
files = new FileInputStream("E:\\javalianxi\\2.mp3");
filess = new FileInputStream("E:\\javalianxi\\3.mp3");
method_2(file,files,filess);
}
catch (IOException e)
{
System.out.println("文件不存在");
}
}
public static void method_2(FileInputStream file,FileInputStream files,FileInputStream filess) throws IOException
{
Vector<FileInputStream> vt = new Vector<FileInputStream>();//Vector中的elements方法能返回Enumeration型的参数
vt.add(file);//添加类元素
vt.add(files);
vt.add(filess);
Enumeration<FileInputStream> et = vt.elements();
SequenceInputStream sqis = new SequenceInputStream(et);//串联其他流通过 Enumeration类型添加流对象
int num = 0;
byte [] bt = new byte[1024];//字节缓冲区
BufferedOutputStream bfps = null;
try
{
while ((num = sqis.read(bt))!=-1)
{
bfps = new BufferedOutputStream(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\我想你.mp3"));//指定输出的流文件
bfps.write(bt,0,num);
}
}
catch (IOException e)
{
System.out.println("错错错");
}
finally
{
if(sqis!=null)
sqis.close();//关闭流资源
if(bfps!=null)
bfps.close();
}
}
}
|
|