aspose file tools
The moose likes Java in General and the fly likes Binary file read/write Question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Binary file read/write Question" Watch "Binary file read/write Question" New topic
Author

Binary file read/write Question

santosh kulkarni
Greenhorn

Joined: Jul 21, 2004
Posts: 18
I am using the following code to read in a PDF file and writing the contents out to a different file. I am expecting that I should be able to
read the second file using the Acrobat reader. I can but the contents are blank! The original file is a proper PDF with text and images.Am I missing something obvious?
Everything is on the same system(same charset etc.)


Impossible is not a fact its an opinion
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16487
    
    2

Converting the (binary) bytes to String data and back to bytes again assumes that the bytes represent text. If they don't, then changes may occur that damage the document (as in this case).

So don't use String or StringBuffer. Just read the bytes from one file and write them to the other file.

Personally I wouldn't use RandomAccessFiles for this task. I would use a BufferedInputStream to read into a buffer of something like 2048 bytes and a BufferedOutputStream to write that buffer into. Keep doing that (in a loop) until you have copied the entire file.
santosh kulkarni
Greenhorn

Joined: Jul 21, 2004
Posts: 18
Thanks for the prompt reply Paul. your comments make sense.
Allow me to elaborate though.
I simplified the code before posting.
What I want to do is Base64 encode the pdf, send it over a web service and
decode and be able to view/print it on the other side.
I was able to do this when I started from a already encoded pdf as a
(java.lang.)String and got the bytes and wrote a file and poof it came up.
I'll keep trying, may be store it in arraylist or something
S
Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
Hi,

If you implement the steps you mentioned as they should, I feel that solution would work smooth. The important thing to keep in mind is that to read the input pdf and write the output pdf in binary format (you should use streams instead of Reader or Writers). I tried it out with sample pdf (around 61kb which had some images too) and the below code worked fine in creating a duplicate pdf.

santosh kulkarni
Greenhorn

Joined: Jul 21, 2004
Posts: 18
Thanks Santhosh, I was trying to avoid pre-determined buffer size. I realized, in my case and most of the cases 512K(or some other simlar number) ought to be more than enough.
Santosh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Binary file read/write Question
 
Similar Threads
encrypt data
Unable to Print more than 1 File
Unable to Print more than 1 File
problem reading output of a subprocess
encrypt data in database