本文介绍了在Java中合并多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要合并的文件数组.这是我尝试过的,但是没有用.

I have an array of files that i want to merge. This is what I tried but it did not 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]);
    }
}

推荐答案

使用 IOUtils 进行此操作.看看我的例子:

Use IOUtils to do 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);
        }
    }
}

如果您不能使用IOUtils lib,请编写自己的实现.示例:

If you can't use IOUtils lib, then 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();
        }
    }
}

这篇关于在Java中合并多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 19:10