0

I have split a file into two files and then if I try merging them back i am not able to open that file. I'm having error of "There was an error opening this document. The file is damaged and could not be repaired."

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class combinefiles {

    public static void main(String[] args) throws IOException {

        File f1 = new File("C:\\Users\\Desktop\\copie1.pdf");
        InputStream if1 = new FileInputStream(f1);
        BufferedInputStream bf1 = new BufferedInputStream(if1);


        File f2 = new File("C:\\Users\\Desktop\\copie2.pdf");
        FileInputStream if2 = new FileInputStream(f2);
        BufferedInputStream bf2 = new BufferedInputStream(if2);

        File f3 = new File("C:\\Users\\Desktop\\merge.pdf");
        FileOutputStream of3 = new FileOutputStream(f3);
        BufferedOutputStream bf3 = new BufferedOutputStream(of3);


        int packetsize = 1024;
        double nosofpackets=Math.ceil(((int) (new File("C:\\Users\\Desktop\\NAV_7.pdf")).length())/packetsize);
        System.out.println(nosofpackets);

        int bytesRead =0;
        byte[] buffer = new byte[1024];

        for ( int i = 0;i<100;i++){
            //while ( (bytesRead = bf1.read(buffer)) != -1){

            bf1.read(buffer, 0,buffer.length);
            //System.out.println("Packet:"+(i+1));
            bf3.write(buffer, 0, buffer.length);
            bf3.flush();
        }



        //while ( (bytesRead = bf2.read(buffer)) != -1){
        for ( int i = 101;i<nosofpackets+2;i++){
            bf2.read(buffer, 0,buffer.length);
            System.out.println("Packet:"+(i+1));
            bf3.write(buffer,0, buffer.length);
            bf3.flush();
        }
        of3.close();        
    }
}
A4L
  • 17,353
  • 6
  • 49
  • 70
cool_guy
  • 1
  • 5
  • I don't think you can just stick the bytes from two PDFs into one file and have a merged file. I suspect it's a little more complicated than that. – Boris the Spider Jun 02 '13 at 17:35
  • How did you split the files? with a pdf tool or just bytewise just to have a bigger file broken up into smaller chunks? Are you expecting the part files `copie1.pdf` and `copie2.pdf` to be valid pdf files? – A4L Jun 02 '13 at 17:38
  • Check this post : http://stackoverflow.com/questions/3585329/how-to-merge-two-pdf-files-into-one-in-java – Juned Ahsan Jun 02 '13 at 17:42

1 Answers1

0

When you read data from a stream, the read method may not be able to fill the buffer with data completely. It returns the number of bytes that it actually read. It also signals the end of a file with the return value -1. You can't just ignore the return value.

        bytesRead = bf1.read(buffer, 0,buffer.length);
        if (bytesRead < 0) {
            // handle end of file!
        }
        bf3.write(buffer, 0, bytesRead);
Joni
  • 108,737
  • 14
  • 143
  • 193