合并java中的文件

时间:2013-02-03 17:16:54

标签: java arrays file merge append

  

可能重复:
  merging file in java

我有一个文件数组,我想将它们合并到一个文件中。我用过这个,但这不起作用

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

1 个答案:

答案 0 :(得分:0)

使用:

    joinf(b[i],a);

而不是

    joinf(a,b[i]);