本帖最后由 罗全涛 于 2011-12-28 14:27 编辑
晚上看SequenceInputStream合并多个(超过两个)文件时,需要一个enumeration 对象作为参数传递进来,视频中讲了两种方式,如下图,不知道还有什么方式可以产生enumeration对象?对JDK文档不是很熟,所以只能看到一种学一种了?希望大家多多帮忙
方式1:
public static void main(String[] args) throws IOException
{
Vector<FileInputStream> v = new Vector<FileInputStream>();
v.add(new FileInputStream("c:\\workspace\\27\\SequenceInputStream\\1.txt"));
v.add(new FileInputStream("c:\\workspace\\27\\SequenceInputStream\\2.txt"));
v.add(new FileInputStream("c:\\workspace\\27\\SequenceInputStream\\3.txt"));
Enumeration<FileInputStream> en = v.elements();
SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream("c:\\workspace\\27\\SequenceInputStream\\4.txt");
byte[] buf = new byte[1024];
int len = 0;
while((len=sis.read(buf))!=-1)
{
fos.write(buf,0,len);
}
fos.close();
sis.close();
}
方式二:
public static void merge()throws IOException
{
ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
for(int i=1; i<5; i++)
{
al.add(new FileInputStream("d:\\temp\\"+i+".part"));
}
final Iterator<FileInputStream> it = al.iterator();
Enumeration<FileInputStream> en = new Enumeration<FileInputStream>()
{
public boolean hasMoreElements()
{
return it.hasNext();
}
public FileInputStream nextElement()
{
return it.next();
}
};
SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream("d:\\temp\\0.mp3");
byte[] buf = new byte[1024];
int len = 0;
while((len=sis.read(buf))!=-1)
{
fos.write(buf,0,len);
}
fos.close();
sis.close();
} |
|