-1

I wrote the code write all pdf files in folder get bytes and write in .dat file.. Acutally its working and writing all bytes in .dat file but When I open that .dat file with Acrobat it open with blank page.... Actually it should open first page, in acrobat right?? Please help me.. Thanks!! other pages can't be open because of header.. but first page should open right??

Here is my code..

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;

public class xmlfile1filebytes {

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

    File folder = new File ("07072013");
    File[] listOfFiles = folder.listFiles();

    System.out.println("There are " + listOfFiles.length + " files"); 
    String filesin;

    String timeStamp = new SimpleDateFormat("MM-dd-yyyy[HH.mm.ss]")
     .format(Calendar.getInstance().getTime());
     System.out.println(timeStamp);

    BufferedWriter xmlfile = null;
    BufferedWriter datfile = null;

    String outxmlfile = ("07072013\\" + timeStamp + ".xml");
    xmlfile = new BufferedWriter(new FileWriter(outxmlfile));

    String outdatfile = ("07072013\\" + timeStamp + ".dat");
    datfile = new BufferedWriter(new FileWriter(outdatfile));

    int offset = 0;
    int size = 0;

    for (int i = 0; i < listOfFiles.length; i++) {

        File f = listOfFiles[i];

       // System.out.println(i + " " + f.getAbsolutePath());
        if (f.isFile()) {

            filesin = listOfFiles[i].getName();

            if (filesin.endsWith("pdf")) {

                Path aPath = Paths.get(f.getAbsolutePath()); 

                System.out.println(filesin);

                byte[] actualBytes = Files.readAllBytes(aPath);
                size = actualBytes.length;

                xmlfile.append((i + 1) + ")" + " File = " + filesin + ", Offset = " + offset + ", Size = " + size + "\n");


                offset = offset + size;
                xmlfile.newLine();

                String s = new String(actualBytes);

                datfile.append(s);
                datfile.newLine();


                File datfileinfolder = new File ("07072013\\" + timeStamp + ".dat");

                long datfilesize = datfileinfolder.length();
                final int BLOCK_SIZE = 200 * 1024;

                for (int curBlock = 0;  curBlock < actualBytes.length; curBlock += BLOCK_SIZE) {
                    String toWrite = new String(
                            Arrays.copyOfRange(actualBytes, curBlock, Math.min(curBlock + BLOCK_SIZE, actualBytes.length)));

                     String suffix = "";

                     if (curBlock > 0) {
                         //append underscores other file information and then perform writes
                         suffix =  String.valueOf(curBlock /  BLOCK_SIZE);
                     }    

                     BufferedWriter datfile1 = null;
                     String outdatfile1 = ("07072013\\" + suffix + timeStamp + ".dat");
                     datfile1 = new BufferedWriter(new FileWriter(outdatfile1));


                     datfile1.append(toWrite);
                     datfile1.close(); 

                }

                //long datfilesizeinkb = datfilesize /1024;

                //System.out.println("Size = " + datfilesizeinkb);



             }
        }
    }
     datfile.close();
     xmlfile.close();
  }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jayraj Patel
  • 79
  • 1
  • 10
  • -1 - duplicate of http://stackoverflow.com/questions/17578374/java-writing-all-pdf-files-bytes-in-dat-file?rq=1 from same author – mschenk74 Jul 12 '13 at 08:44

2 Answers2

0

You are using a Writer where a OutputStream is the right choice. A Writer does character encoding and maybe line ending conversion which may damage binary data.

By the way: It is inefficient to reopen and close your output file for each data block you write.

Michael Butscher
  • 10,028
  • 4
  • 24
  • 25
0

A bit of advanced code, but wrong at some places. Binary data should use an OutputStream, not a Writer. new String(actualBytes); uses the current platform encoding, and one would like to have it platform independant. On Linux that often is UTF-8, and in UTF-8 not all byte sequences form a legal String, better new String(actualBytes, "Windows-1252"); - but that is hack on hack.

To your purposes a .zip file seems best. Simply adding the content of several PDF files to one file will not create an entire one. If you give it the ending .pdf, maybe some PDF viewer might display it (even though there is garbage at the end: the other PDFs).

To create a zip file either use the old API with a ZipFile/ZipOutputStream, or use a Zip FileSystem of Java 7.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138