Skip to content

Instantly share code, notes, and snippets.

@fedcba98
Last active August 29, 2015 13:58
Show Gist options
  • Save fedcba98/9939286 to your computer and use it in GitHub Desktop.
Save fedcba98/9939286 to your computer and use it in GitHub Desktop.
package ua.rumata.copyfiles;
import java.io.*;
import java.util.ArrayList;
import java.util.Vector;
public class SequenceCombineFiles {
public static void main(String[] args) {
String[] filesToCombine = { "someFolder/part1.txt", "someFolder/part2.txt" };
File destFile = new File( "someFolder/combined.txt" );
try ( FileOutputStream out = new FileOutputStream( destFile ) ) {
ArrayList<InputStream> streams = new ArrayList<>();
for( String fileName : filesToCombine ) {
try ( FileInputStream stream = new FileInputStream( new File( fileName ) ) ) {
streams.add( stream );
}
}
try ( SequenceInputStream seqIS = new SequenceInputStream( new Vector<>( streams ).elements() ) )
{
byte[] buffer = new byte[1024];
int remain;
while ((remain = seqIS.read(buffer)) > 0) {
out.write( buffer, 0, remain );
}
}
System.out.println( "Files combined successfully" );
} catch ( IOException e ) {
System.out.println( e.getMessage() );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment