27.04.2013 Views

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

String, text, numbers, I/O II part<br />

the creation of the file if it does not exist are a single operation that is atomic with<br />

respect to all other filesystem activities that might affect the file. This method, in<br />

combination with the deleteOnExit() method, can therefore serve as the basis for a<br />

simple but reliable cooperative file-locking protocol."<br />

Q: How do I write an image to stdout? This is for a cgi program that will return an<br />

image to a browser.<br />

For example, if a user click on a link to http://somewhere.edu/cgi/getpix.cgi the<br />

browser will receive image data from the cgi program and will display the image.<br />

Answer: Send "Content-type: image/gif" - and then send the data.<br />

Alternatively, send an HTML page, with a pointer to the (possibly-freshly-written)<br />

image on the server.<br />

--<br />

Tim Tyler Try my latest game - it rockz - http://rockz.co.uk/<br />

Q: Does anyone know how I can get around the fact that a Message obj (from<br />

the <strong>Java</strong>Mail API) is not serializable?<br />

I would like to save a message obj to the hd, then read the file (is 'file' the wrong<br />

word here?) back to my e-mail client and read it as a Message obj.<br />

Answer: The obvious answer is: implement serializable yourself in a subclass!<br />

But! That won't work. The serialization mechanism will throw an exception at runtime.<br />

Even if it didn't, the class may be nonserializable for a good reason, for example it<br />

might contain a socket, which can't be reinitialized from a stream.<br />

What you need is a subclass that implements the Externalizable interface, and then<br />

your own code that saves as serializable objects those attributes of the object that<br />

you need to reconstitute it later. I'm not familiar with this particular object, but I<br />

imagine it has several get() methods that return serializable objects like Strings or<br />

ints. What you have to do is recover those objects and put them into the output<br />

stream yourself, and reverse the process on unserialization. The fields that are not<br />

serializable you ignore.<br />

You have to provide a no-argument constructor that reconstructs anything you aren't<br />

going to serialize (for example, opening a new socket). Read about the<br />

Externalizable interface for more details.<br />

--<br />

Frank LaRosa<br />

Q: Is it possible to send Vector class in socket communication. If it is, what<br />

function can make it.<br />

Answer: Make sure the objects contained in the Vector are Serializable. Look at<br />

java.io.ObjectOutputStream for serializing the Vector by wrapping the output stream<br />

from socket with the ObjectOutputStream...<br />

Q: How can you tell if an integer is odd or even?<br />

I know an even number is divisible by 2 but I'm thinking then how can I detect<br />

if a resulting number after dividing 2 ints has a remainder?<br />

Answer: Basically the method is simple, if a variable contains an odd number<br />

I want to return the value 'zero', and on the other hand if the<br />

variable contains an even number I want to return the value 'one'.<br />

file:///F|/350_t/350_tips/stings_text__date_numbers_io-II.htm (4 of 7) [2002-02-27 21:19:21]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!