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.

General <strong>Java</strong> Questions I<br />

I could not quite get it using the ZipOutputStream because it simply creates a new file<br />

and write only that entry for me. The rest of the original entries are gone.<br />

Does anyone have a solution for this?<br />

Answer:<br />

1) Read the file (myfile.properties) out of the original Zip Archive (original.zip)<br />

2) Make your changes, write the file to the file system<br />

3) Create a New Archive (originalNew.zip)<br />

4) Write your edited file (myfile.properties) to originalNew.zip<br />

5) loop through all the entries in the original Zip archive (original.zip), adding them to<br />

the new archive (originalNew.zip) EXCEPT for the file you are replacing<br />

(myfile.properties)<br />

6) When you're done, erase the original and rename your new one to original.zip.<br />

I believe that this may be the only way to do this, since there doesn't seem to be any<br />

random access in the ZIP file.<br />

--<br />

Kevin T. Smith<br />

Q: About the order of my elements in hastable...<br />

I save some data into hashtable.<br />

For example,<br />

hashtable.put ( "1", "one" );<br />

hashtable.put ( "2", "two" );<br />

hashtable.put ("3" , "three" );<br />

when I get back the element from hashtable using Enumeration class ,<br />

Enumeraton e = hashtable.keys();<br />

while ( e.hasMoreElements() ){<br />

Object k = e.nextElement();<br />

Object v = hashtable.get (k );<br />

}<br />

the default result is<br />

"3","three"<br />

"2", "two"<br />

"1", "one"<br />

here I want to get the data sorted as ascending. ( the order as I insert ) such as<br />

"1", "one"<br />

"2", "two"<br />

"3", "three"<br />

or is it possible to get the data from end of hashtable?<br />

Answer: When you insert elements into a Hashtable, they will not be stored in the<br />

order you insert them. They are stored in a way that makes it easy to find them by the<br />

key you specify.<br />

So, you must either use another data structure (e.g. Vector) or sort them after you<br />

retrieve them from the Hashtable (e.g. by using java.util.Collections, java.util.Arrays).<br />

--<br />

Marco Schmidt<br />

file:///F|/350_t/350_tips/general_java-I.htm (23 of 31) [2002-02-27 21:18:17]

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

Saved successfully!

Ooh no, something went wrong!