I have a file in parts. I have to merge them all to in file. I am merging them using RandomAccessFile and it's working fine but for larger files it is very slow.
Here is the code I am using for merging them:
RandomAccessFile outFile = new RandomAccessFile(filename, "rw");
long len = 0;
//inFiles is a LinkedList<String> conatining all file part names
for (String inFileName : inFiles) {
RandomAccessFile inFile = new RandomAccessFile(inFileName, "r");
int data;
outFile.seek(len);
while ((data = inFile.read()) != -1) {
outFile.writeByte(data);
}
len += inFile.length();
inFile.close();
}
outFile.close();
Is there any other way of merging the files that may be faster than this method?... Please help me optimize this code.
As Nemo_64 points out, you are using read() byte at a time which will be very slow on large files. As you are not really using RandomAccessFile for random access, just using sequential stream IO would be enough, something like:
try(OutputStream out = Files.newOutputStream(Paths.get(filename), StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
for (String inFileName : inFiles) {
Files.copy(Paths.get(inFileName), out);
}
}