Reputation: 1203
I am trying to concatenate a set of text files using the following method. However, only the first file is show in the output file.
public void concatenateFiles(List<String> fileLocations, String outputFilename){
try(FileChannel outputChannel = new FileOutputStream(outputFilename).getChannel()) {
long position = 0;
for(String fileLocation: fileLocations){
try(FileChannel inputChannel = new FileInputStream(new File(fileLocation)).getChannel()){
position += inputChannel.transferTo(position, inputChannel.size(), outputChannel);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Do you see any problems?
Upvotes: 1
Views: 712
Reputation: 5797
Change
position += inputChannel.transferTo(position, inputChannel.size(), outputChannel);
to
position += inputChannel.transferTo(0, inputChannel.size(), outputChannel);
The first parameter is a start position for reading inputChannel
Upvotes: 3