【问题标题】:Merging multiple files in java [closed]在java中合并多个文件[关闭]
【发布时间】:2013-01-18 08:17:09
【问题描述】:

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

 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]);
    }
}

【问题讨论】:

  • 那么上面的代码有什么问题呢?
  • 如果只想解决问题可以使用stackoverflow.com/questions/2477271/… 或者 cat file1 file2 ... filen > concatenated_file
  • @awregan - 他想用 Java 来做...

标签: java


【解决方案1】:

在其当前形式中,您的 pro 方法将文件 a 附加到每个 b 文件中。原因是您的joinf(a, b) 方法将文件a 附加到文件b,而不是相反。

要修复它,这个:

joinf(a,b[i]);

应该改成这样:

joinf(b[i], a);

或类似的东西。


仅供参考:您编写的代码中还有其他各种问题。请检查它们,以便您下次可以编写更好的代码...

  • 在某些情况下,您的 joinf 方法可能会泄漏文件描述符 - 应在 finally 中关闭(显式或隐式)资源。
  • 在这样的库方法中调用 System.exit() 会使其难以重用。
  • 您正在丢弃堆栈跟踪。这将使您的应用的现场部署副本的调试变得困难。
  • 处理IOException的策略错误。如果你在那里得到了一个意外的 IOException,那就是大错特错了。打印并继续是一个非常糟糕的主意。
  • 名称projoinf 是糟糕的选择。
  • 没有 javadocs

【讨论】:

  • 但我想将所有 b 文件附加到一个文件中
  • 好的,继续。他只是告诉你出了什么问题。
  • @user1781384 - “但我想将所有 b 文件附加到一个文件中” - 我刚刚解释了为什么你的程序不这样做,并为您提供了如何修复它的建议。如果你不明白,请说你不明白的。
【解决方案2】:

使用IOUtils 执行此操作。看我的例子:

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 库,请编写您自己的实现。示例:

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();
        }
    }
}

【讨论】:

  • 我不能使用这个import org.apache.commons.io.IOUtils;
  • 为什么不能使用?这是非常流行的库。
  • 但它最终给了我错误{ IOUtils.closeQuietly(output); } 我该怎么办?
  • 请帮帮我,这对我来说很重要
  • 你有什么问题?对我来说一切正常。
猜你喜欢
  • 2014-01-29
  • 1970-01-01
  • 1970-01-01
  • 2016-01-26
  • 2013-11-20
  • 2011-06-14
  • 2019-07-13
  • 2011-07-27
  • 2016-06-15
相关资源
最近更新 更多