SequenceInputStream(Enumeration<? extends InputStream> e) SequenceInputStream(InputStream s1, InputStream s2)
Example: Combine multiple files into one stream and write to one file:
Vector<FileInputStream> v = new Vector<FileInputStream>(); v.add(new FileInputStream("c:\\1.txt")); v.add(new FileInputStream("c:\\2.txt")); v.add(new FileInputStream("c:\\3.txt")); Enumeration<FileInputStream> en = v.elements(); SequenceInputStream sis = new SequenceInputStream(en); FileOutputStream fos = new FileOutputStream("c:\\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();
Merge files
ArrayList<FileInputStream> aList = new ArrayList<FileInputStream>();// Why use list, because Vector is outdated. for (int x = 1; x <= 4; x++) { aList.add(new FileInputStream("d:\\tmp\\" + x + ".part"));// Add the part files under d:\\tmp\\ to aList cyclically } final Iterator<FileInputStream> it = aList.iterator(); Enumeration<FileInputStream> en = new Enumeration<FileInputStream>() { @Override public FileInputStream nextElement() { // TODO Auto-generated method stub return it.next(); } @Override public boolean hasMoreElements() { // TODO Auto-generated method stub return it.hasNext(); } }; SequenceInputStream sis = new SequenceInputStream(en); FileOutputStream fos = new FileOutputStream("d:\\tmp\\aa.exe"); byte[] buf = new byte[1024 * 1024]; int len = 0; while ((len = sis.read(buf)) != -1) { fos.write(buf, 0, len); } fos.close(); sis.close();
cut file
FileInputStream fis = new FileInputStream("d:\\tmp\\plsqldev.exe"); FileOutputStream fos = null; byte[] bytes = new byte[1024 * 1024 * 3]; int len = 0; int count = 1; while ((len = fis.read(bytes)) != -1) { fos = new FileOutputStream("d:\\tmp\\" + (count++) + ".part"); fos.write(bytes, 0, len); } fos.close(); fis.close();