Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Easy Learn Java: Programming Articles, Examples and Tips - Page 147


Previous 1060 Stories (530 Pages, 2 Per Page) Next

Java Newsletters: Java Newsletters Archive: 16

Go to all tips in Java Newsletters

***************************************

* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *
* > The Java FAQ Daily Tips, weekly publication < *
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *
* *
* Issue No: 16 20 December 2000
* http://www.javafaq.nu/java
*
*
* Please recommend our FREE "100 Java Tips" book and us to your
* friends and colleagues!
* http://javafaq.nu/java/advert/our_book.shtml
***************************************

Table of Contents

1. I want to use more fonts that which is not avilable in the
present jdk...
2. What're the differences between classpath and import...
3. What must be done to reduce size of a jar file?
4. Is it cls-like command in DOS (CMD) window? I would like
to clean the screen the user sees?
5. What is difference between Iterator and Enumeration?
6. How can I find the first dimension length of the
2-dimenstions array?
7. I don't know anything about security...
***************************************

Hello dear friends!

Merry Christmas! And wish you to have good holidays!
You can to present your colleagues very good Christmas gift -
our "100 Java Tips" book!
If you have your good tips send us and we will add it to our free
book! One sunny day when your friends will load the new version of
our book they will find your name there also!


***************************************

Tip 1 Q: I want to use more fonts in my applet... say for example
Arial... which is not avilable in the present jdk package...
How can i deal with it?

answer: import java.awt.Toolkit;
....
Toolkit tools : new Toolkit();
String[] fontList = tools.getFontList();


***************************************

Tip 2 Q: What're the differences between classpath and import in
the java application?

Do I need to have the package in the classpath first before
importing in a java application or need not?

Answer: Classpath is an environment variable of your OS, you have
to set it (or better: Java sets it) to tell Java where to search
for classes. 
You use import in a program to let Java search for the specified
classes within the classpath. This implies that the package must
be in the classpath.
Stephan Effelsberg

***************************************

Tip 3 Q: What must be done to reduce size of a jar file?
What optimization techniques to use on classes inside the jar file?
What tools if any?

Answer: A JAR file is a ZIP archive. You can influence its size by
choosing the degree of compression you want to have. This is
usually defined by a value between 0 (no compression) and 9
(maximum compression). Although JAR tool does not list a -9 switch,
you might want to create compressed JARs with any ZIP tool like
Winzip or the free Info-ZIP zip command line tool. 
The amount of reduction you get totally depends on the nature of
your data. 
Note that if you use compression in your JAR file, loading goes
slower (classes must be decompressed).

***************************************

Tip 4 Q: Is it cls-like command in DOS (CMD) window? I would like
to clean the screen the user sees?

Answer: Unfortunately there is no command as "cls", but try this
instead:

for (int i=0; i<25; i++) System.out.println();

It will print 25 empty lines and they will move current printouts
up.


Tip 5 Q: What is difference between Iterator and Enumeration?

First of all Java FAQ Team wish you !!!HAPPY NEW YEAR!!! and then
Answer: from
http://java.sun.com/docs/books/tutorial/collections/interfaces/collection.html

The object returned by the iterator method deserves special
mention. It is an Iterator, which is very similar to an Enumeration,
but differs in two respects: 
Iterator allows the caller to remove elements from the underlying
collection during the iteration with well-defined semantics. 
Method names have been improved. 
The first point is important: There was no safe way to remove
elements from a collection while traversing it with an Enumeration.
The semantics of this operation were ill-defined, and differed from
implementation to implementation. 
The Iterator interface is shown below: 

public interface Iterator {
boolean hasNext();
Object next();
void remove(); // Optional
}

The hasNext method is identical in function to
Enumeration.hasMoreElements, and the next method is identical in
function to Enumeration.nextElement. The remove method removes
from the underlying Collection the last element that was returned
by next. The remove method may be called only once per call to
next, and throws an exception if this condition is violated. Note
that Iterator.remove is the only safe way to modify a collection
during iteration; the behavior is unspecified if the underlying
collection is modified in any other way while the iteration is
in progress. 
The following snippet shows you how to use an Iterator to filter
a Collection, that is, to traverse the collection, removing
every element that does not satisfy some condition: 

static void filter(Collection c) {
for (Iterator i = c.iterator(); i.hasNext(); )
if (!cond(i.next()))
i.remove();
}

Two things should be kept in mind when looking at this simple
piece of code: 
1. The code is polymorphic: it works for any Collection that
supports element removal, regardless of implementation. That's
how easy it is to write a polymorphic algorithm under the
collections framework! 
2. It would have been impossible to write this using Enumeration
instead of Iterator, because there's no safe way to remove an
element from a collection while traversing it with an Enumeration.

***************************************

Tip 6 Q: How can I find the first dimension length of the
2-dimenstions array? I have use the array[].length but it does
not work, how can I solve this problem? 

Answer: Java doesn't really have "multidimensional arrays",
only arrays of arrays. So try: array[0].length and you will get
this dimension.

***************************************

Tip 7 Q: I need to read C: est.doc for example and store it
somewhere for my future use. I don't know anything about security.
Part 1.

Answer: If you want to read a file on the local file system in
an applet, you are going to digitally sign the applet, and the
user of the applet is going to indicate trust in the signature.
How you go about this depends on a number of questions, like:
In which browser(s) will the users be running the applet?
Will a Java plug-in be installed in the browser?
Can the user install a plug-in?

Here are a few tips on some of the sticky points of signing
applets...
-------------------------------------------------------------------

If you are using the jar signing tools that come with the JDK,
you'll find that they will only work with the Java plug-in.
Netscape and Microsoft IE don't salute the Sun way of signing
applets.

If you can rely on your users to install the Java plug in, you
can sign applets using the JDK 1.1.X javakey command. You will
also have to have them import your certificate into their
'identitydb.obj' file in their home directory, and mark it as
trusted. If importing the certificate is too much to ask, and
it usually is, you could create an 'identitydb.obj' containing
the certificate, already marked as trusted, and have them
download this to their home directory. Be careful not to
distribute the 'identitydb.obj' that you used to sign the jar,
since that would allow anyone to sign code as you!

JDK1.2.X works in a similar way, but the 'javakey' command has
been replaced by 'jarsigner' and 'keytool'. In addition, 1.2
adds granularity to the security model, so that you can assign
privileges outside of the sandbox via a policy file. For example
you could allow an applet to read files, but not write them.
JDK 1.1.X security is kind of an all or nothing proposition.

If you can't count on the Plugin being there, but you know your
users will be using Netscape, you can sign use the Netscape
signtool. Netscape's signtool is a little different than Sun's.
With Netscape's, you put all of your class files into a
directory, like signdir, and then run something like this: 
./signtool -k MyCert -Z MyApplet.jar signdir
The 'MyApplet.jar' file will be created, and, of course, you
would already have to have MyCert in your security database.
You can generate a test certificate through signtool, but it's
only good for a couple of months.
Joe Morrogh - Excite@Home -
The Leader in Broadband http://home.com/faster
Part 2 of this tip will be published next week!


The Java FAQ Daily Tips is a newsletter that is only sent to those
who have specifically subscribed to it.

John Andersson,

Copyright (c) 2000 John Andersson
 



13 comments | Printer Friendly Page  Send to a Friend | Java Newsletters | Score: 5
Posted by jalex on Monday, February 02, 2004 (15:07:08) (2862 reads)

Java Newsletters: Java Newsletters Archive: 15

Go to all tips in Java Newsletters
New Page 1

**********************************************
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *
* > The Java FAQ Daily Tips, weekly publication < *
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *
*
* Issue No: 15 14 December 2000
* http://www.javafaq.nu/java
*
*
* Please recommend us and our FREE "100 Java Tips" book to your
* friends and colleagues!
* http://javafaq.nu/java/advert/our_book.shtml
**********************************************

Table of Contents

1. I want to send a POST request, but I can't find such
functionality in the servlet API, how can I do this?
2. How do I delete a file in Java?
3. How can I get an IP Adress in the class InetAdress? The
constructor is private, so I can’t use it.
4. Is there a mod (x, y) function that returns the remainder when
x is divided by y?
5. This SUPER tip you can read just on our site 24 December!
6. What is difference capacity() and size() methods for vector?
7. How do I generate mouse click event but without a real click?
**********************************************

Hello dear friends!

We published again pdf-version (Acrobat Reader) of our "100 Java Tips"
http://javafaq.nu/java/advert/our_book.shtml
We made more descriptive contents: instead of "Tip 1" you can see
"Tip 1. How I can ..." and also made small corrections.
We continue our job on book updating and will add new tips in it.


By the way, I found very useful free application - Zapper that
helps to make life much easier and offers a new, more efficient
way to get the information you need from the Web.
It takes you from wherever you are on your desktop to exactly
what you're looking for on the Web - in just one click of your mouse!


**********************************************

Tip 1 Q: I want to send a POST request, but I can't find such
functionality in the servlet API, how can I do this? Must I
implement this with a socket connection to port 80?

Answer: A servlet can do anything a standalone Java application
can do. It doesn't need anything beyond what the java.net package
already provides. You can use an httpURLConnection to POST to a
server program like a servlet or CGI script:

Create a string with the parms you want to post and convert it to
a byte array. You may need to pass the values through
java.net.URLEncoder.encodeURL(). If they have embedded blanks
or special characters

String parms = "a=10" + "&b=20" + "&c=30";
byte[] bytes = parms.getBytes();

Create a URL pointing to the servlet or CGI script and open an
HttpURLConnection on that URL

URL url = new URL(TARGET_URL);
HttpURLConnection con = (HttpURLConnection) url.openConnection();

Indicate that you will be doing input and output, that the method
is POST, and that the content length is the length of the byte
array

con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Content-length",
String.valueOf(bytes.length));

// Write the parameters to the URL output stream

OutputStream out = con.getOutputStream();
out.write(bytes);
out.flush();
// Read the response
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
while (true) {
String line = in.readLine();
if (line == null) break;
System.out.println(line);
}
in.close();
out.close();
con.disconnect();

--
Phil Hanna
Author of Instant Java Servlets
http://www.philhanna.com

**********************************************
Tip 2 Q: How do I delete a file in Java? I have programmed a Java
application that needs to delete a file, but I couldn't find
anything on the topic.

Answer: in java.io.File there is delete
public boolean delete()

Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted. Create a new File object representing the file, and then use the delete () method. If you use the Microsoft JVM, make sure the file is not shared before you delete it, otherwise that will not work (the delete () method returns "false").

**********************************************

Tip 3 Q: How can I get an IP Adress in the class InetAdress? The
constructor is private, so I can’t use it. I want to call the
method getName () to get the domain name out of an IP Adress.

A: It is not necessary to construct something Smile
Just do it like this:
String hostname = InetAddress.getLocalHost().getHostName();


**********************************************

Tip 4 Q: Is there a mod (x, y) function that returns the
remainder when x is divided by y? Something equivalent to
fmod(x,y) in C?

Answer: a = x%y;

**********************************************

Tip 5 This SUPER tip you can read just on our site 24 December!

**********************************************

Tip 6 Q: What is difference between

public final int capacity()
Returns the current capacity of this vector.

and

public final int size()
Returns the number of components in this vector.

Answer: please read this method: ensureCapacity
public final synchronized void ensureCapacity(int minCapacity)

Increases the capacity of this vector, if necessary, to ensure
that it can hold at least the number of components specified
by the minimum capacity argument.
Parameters:
minCapacity - the desired minimum capacity.

**********************************************

Tip 7 How do I generate mouse click event but without a real click?
Q: Could you tell me how I can generate an event (like a mouse
click) from a Java application? (I want generate a mouse click
event but without a real click on the mouse).

Answer: See "simulate keyboard pressing" below: You can use
java.awt.Robot" since java1.3


The Java FAQ Daily Tips is a newsletter that is only sent to those
who have specifically subscribed to it.

John Andersson,
Copyright (c) 2000 John Andersson
**********************************************
* You can find our tips on site also! *
* Please recommend us and our FREE "100 Java Tips" book to your *
* friends and colleagues! *
* http://javafaq.nu/java/advert/our_book.shtml *
* *
**********************************************



18 comments | Printer Friendly Page  Send to a Friend | Java Newsletters | Score: 0
Posted by jalex on Monday, February 02, 2004 (14:56:08) (2984 reads)

Previous 1060 Stories (530 Pages, 2 Per Page) Next

530| 529| 528| 527| 526| 525| 524| 523| 522| 521| 520| 519| 518| 517| 516| 515| 514| 513| 512| 511| 510| 509| 508| 507| 506| 505| 504| 503| 502| 501| 500| 499| 498| 497| 496| 495| 494| 493| 492| 491| 490| 489| 488| 487| 486| 485| 484| 483| 482| 481| 480| 479| 478| 477| 476| 475| 474| 473| 472| 471| 470| 469| 468| 467| 466| 465| 464| 463| 462| 461| 460| 459| 458| 457| 456| 455| 454| 453| 452| 451| 450| 449| 448| 447| 446| 445| 444| 443| 442| 441| 440| 439| 438| 437| 436| 435| 434| 433| 432| 431| 430| 429| 428| 427| 426| 425| 424| 423| 422| 421| 420| 419| 418| 417| 416| 415| 414| 413| 412| 411| 410| 409| 408| 407| 406| 405| 404| 403| 402| 401| 400| 399| 398| 397| 396| 395| 394| 393| 392| 391| 390| 389| 388| 387| 386| 385| 384| 383| 382| 381| 380| 379| 378| 377| 376| 375| 374| 373| 372| 371| 370| 369| 368| 367| 366| 365| 364| 363| 362| 361| 360| 359| 358| 357| 356| 355| 354| 353| 352| 351| 350| 349| 348| 347| 346| 345| 344| 343| 342| 341| 340| 339| 338| 337| 336| 335| 334| 333| 332| 331| 330| 329| 328| 327| 326| 325| 324| 323| 322| 321| 320| 319| 318| 317| 316| 315| 314| 313| 312| 311| 310| 309| 308| 307| 306| 305| 304| 303| 302| 301| 300| 299| 298| 297| 296| 295| 294| 293| 292| 291| 290| 289| 288| 287| 286| 285| 284| 283| 282| 281| 280| 279| 278| 277| 276| 275| 274| 273| 272| 271| 270| 269| 268| 267| 266| 265| 264| 263| 262| 261| 260| 259| 258| 257| 256| 255| 254| 253| 252| 251| 250| 249| 248| 247| 246| 245| 244| 243| 242| 241| 240| 239| 238| 237| 236| 235| 234| 233| 232| 231| 230| 229| 228| 227| 226| 225| 224| 223| 222| 221| 220| 219| 218| 217| 216| 215| 214| 213| 212| 211| 210| 209| 208| 207| 206| 205| 204| 203| 202| 201| 200| 199| 198| 197| 196| 195| 194| 193| 192| 191| 190| 189| 188| 187| 186| 185| 184| 183| 182| 181| 180| 179| 178| 177| 176| 175| 174| 173| 172| 171| 170| 169| 168| 167| 166| 165| 164| 163| 162| 161| 160| 159| 158| 157| 156| 155| 154| 153| 152| 151| 150| 149| 148|
147
| 146| 145| 144| 143| 142| 141| 140| 139| 138| 137| 136| 135| 134| 133| 132| 131| 130| 129| 128| 127| 126| 125| 124| 123| 122| 121| 120| 119| 118| 117| 116| 115| 114| 113| 112| 111| 110| 109| 108| 107| 106| 105| 104| 103| 102| 101| 100| 99| 98| 97| 96| 95| 94| 93| 92| 91| 90| 89| 88| 87| 86| 85| 84| 83| 82| 81| 80| 79| 78| 77| 76| 75| 74| 73| 72| 71| 70| 69| 68| 67| 66| 65| 64| 63| 62| 61| 60| 59| 58| 57| 56| 55| 54| 53| 52| 51| 50| 49| 48| 47| 46| 45| 44| 43| 42| 41| 40| 39| 38| 37| 36| 35| 34| 33| 32| 31| 30| 29| 28| 27| 26| 25| 24| 23| 22| 21| 20| 19| 18| 17| 16| 15| 14| 13| 12| 11| 10| 9| 8| 7| 6| 5| 4| 3| 2| 1|


Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy