Last active
August 29, 2015 13:58
-
-
Save fedcba98/9939286 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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