在java中合并文件 [英] merging file in java

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

问题描述

我有一个文件数组,我想将它们合并到一个文件中
请给我方法
提前谢谢
i使用此但这不起作用

i have an array of files that i want to merge them in one file please give me method thank's in advance i used this but this 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中合并文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆