Merging multiple files in Java
I have an array of files that I want to combine. This is what I tried, but it didn't work.
public static void joinf(File f1, File f2){
try{
InputStream in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2,true);
byte[] buf = new byte[8192];
int len;
while ((len = in.read(buf)) > 0){
out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println("File copied.");
}
catch(FileNotFoundException ex){
System.out.println(ex.getMessage() + " in the specified directory.");
System.exit(0);
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
public void pro(File a,File[]b){
for(int i=0;i<b.length;i++){
joinf(a,b[i]);
}
}
source to share
Use IOUtils for this. See my example:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.io.IOUtils;
public class SourceCodeProgram {
public static void main(String[] args) throws Exception {
IOCopier.joinFiles(new File("D:/d.txt"), new File[] {
new File("D:/s1.txt"), new File("D:/s2.txt") });
}
}
class IOCopier {
public static void joinFiles(File destination, File[] sources)
throws IOException {
OutputStream output = null;
try {
output = createAppendableStream(destination);
for (File source : sources) {
appendFile(output, source);
}
} finally {
IOUtils.closeQuietly(output);
}
}
private static BufferedOutputStream createAppendableStream(File destination)
throws FileNotFoundException {
return new BufferedOutputStream(new FileOutputStream(destination, true));
}
private static void appendFile(OutputStream output, File source)
throws IOException {
InputStream input = null;
try {
input = new BufferedInputStream(new FileInputStream(source));
IOUtils.copy(input, output);
} finally {
IOUtils.closeQuietly(input);
}
}
}
If you can't use the IOUtils lib, write your own implementation. Example:
class IOUtils {
private static final int BUFFER_SIZE = 1024 * 4;
public static long copy(InputStream input, OutputStream output)
throws IOException {
byte[] buffer = new byte[BUFFER_SIZE];
long count = 0;
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
public static void closeQuietly(Closeable output) {
try {
if (output != null) {
output.close();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
source to share
In its current form, your method pro
adds a file a
to each of the files b
. The reason is that your method joinf(a, b)
adds the file a
to the file b
and not vice versa.
To fix this, follow these steps:
joinf(a,b[i]);
should be changed to this:
joinf(b[i], a);
or something like that.
FYI: There are several other problems written in your code. Please review them so you can write better code next time ...
- Your joinf method can leak file descriptors in certain circumstances - resources must be closed (either explicitly or implicitly) in
finally
. - Calling
System.exit()
a library in such a method makes it difficult to reuse. - You are throwing stack traces. This will make it difficult to debug a field copy of your application.
- The strategy for eliminating the IOException is incorrect. If you get an unexpected IOException there, something is seriously wrong. Printing and sequel is a really bad idea.
- The names
pro
andjoinf
are bad. - No javadocs
source to share