aspose file tools
The moose likes Java in General and the fly likes Read and Write PDF file using RandomAccessFile class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Read and Write PDF file using RandomAccessFile class" Watch "Read and Write PDF file using RandomAccessFile class" New topic
Author

Read and Write PDF file using RandomAccessFile class

Natesan Prabhakaran
Ranch Hand

Joined: Jul 11, 2006
Posts: 47
Hi All,

I am new to this API. I have a doubt from RandomAccessFile. Can anyone tell me that, how to read and write PDF files using RandomAccessFile class. Otherwise I need to use any third party APIs.

Thanks in advance.

Regards,
Prabhakaran
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

Why would you want to do that?

And yes, you don't have to use any 3rd-party code if you want to learn and implement the PDF specification yourself. But I wouldn't recommend that unless you have a couple of months with nothing else to do. Using the 3rd-party code is a much better idea.

And why are RandomAccessFile and 3rd-party APIs the only two alternatives?
Natesan Prabhakaran
Ranch Hand

Joined: Jul 11, 2006
Posts: 47
Thanks for your reply.

I am sending PDF (other formats also)file using UDP. If while missing some packets, I need to send back missing packets and write it into PDF file. But PDF format is somehow distrubing to do that.

Also, can you suggest better third-party API for it.

-Prabhakaran
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

UDP makes absolutely no guarantees for any packet arriving safely, so it should not be used for file transfers. Why did you decide to use UDP over TCP in the first place?


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Natesan Prabhakaran
Ranch Hand

Joined: Jul 11, 2006
Posts: 47
But compare to UDP, TCP will take much time to transfer a very large file and huge amount of files at a time. Thats the reason, I go with UDP.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

If you don't care about receiving the whole file, an even faster method is to just not send it at all. Takes zero time, it isn't especially accurate but it's really really fast.

Seriously, like Rob said, don't use UDP.

And how did this question get to be about transmitting data, when your original post was about random access files and products for producing PDF files?
Natesan Prabhakaran
Ranch Hand

Joined: Jul 11, 2006
Posts: 47
Thanks a lot paul.

That’s the reason why, if any packets missed out, I request for missing packets. Then, by using RandomAccessFile, need to send back the missing data only. Also, I want to write it into PDF file in the receiving end.

So, Please tell me, is there any possibility to write on PDF by using RandaomAccessFile, else suggest me an idea.

- Prabhakaran
Yosi Hendarsjah
Ranch Hand

Joined: Oct 02, 2003
Posts: 164
Natesan Prabhakaran wrote:
So, Please tell me, is there any possibility to write on PDF by using RandaomAccessFile

Yes, you can read and write any kind of files using RandomAccessFile. However, if you want to create a PDF file from a string or a text file, you need a Java PDF library. Search in Google using "Java PDF Library" as the keywords.

Regarding using UDP instead of TCP, is it your decision or someone else's (your boss/software architect/team leader)? If it is your own decision, you should re-consider it. Using UDP, not only that you may loss some of the packets, the receiver may receive the packet in a wrong order. And you may receive a single packet twice. Your program must be able to handle these kinds of situation.

There are a lot of articles in the internet about the pros and cons of TCP and UDP. You should read them.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Yosi is right. It is TCP that does all this for you. That's also why it is slower; it sends acknowledgements back to the sender to indicate a packet has been received correctly. That's also something you would need to do. You can't assume you've missed a packet because the following packet has arrived. You can wait until you've received the last packet and then request the missing pieces, but what if this last packet never arrives?

Seriously, use TCP. It will do all the hard work for you.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

Natesan Prabhakaran wrote:Thanks a lot paul.

That’s the reason why, if any packets missed out, I request for missing packets. Then, by using RandomAccessFile, need to send back the missing data only. Also, I want to write it into PDF file in the receiving end.

So, Please tell me, is there any possibility to write on PDF by using RandaomAccessFile, else suggest me an idea.


I don't understand why you think that writing to a file with a RandomAccessFile object is somehow going to re-send missing UDP packets. Where on earth did you get that idea from?

Possibly you could receive the missing packets and update the file on the receiving end with a RandomAccessFile object, if you really kept track of the packets which were received and had some way of identifying the ones which weren't received. But trying to make a reliable protocol out of UDP is pointless when you already have TCP to use. You can wave your hands about speed all you like but you aren't going to be able to make a reliable layer over UDP which is better than TCP.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Read and Write PDF file using RandomAccessFile class
 
Similar Threads
Deleted record
Locking question - URLyBird 1.2.1
Java stored procedure, cannot write file
Binary file read/write Question
java.nio.ByteBuffer?