Posts in the “java” category

Java wget: JGet, something like wget

Java FAQ: Can you share some source code for a “Java wget” program, i.e., a Java program that works like the Unix wget or curl commands?

Here's the source for a program I've named JGet, which acts similar to the wget or curl programs. I didn't have wget installed when I needed it (and my client wouldn't let me install it), so I wrote this Java wget replacement program.

How to write a Java method that returns a generic type (syntax)

As a quick note, if you need some examples of the syntax of how to write a Java method that returns a generic type, I hope these are helpful:

public static <T> T getRandomValue(List<T> listOfPossibleOutcomes, int numPossibilities) {
    int r = RandomNumberGenerator.getRandIntBetween(0, numPossibilities);
    return listOfPossibleOutcomes.get(r);
}

public static <T> T getRandomValueFromGenericList(List<T> list) {
    Collections.shuffle(list);
    return list.get(0);
}

I hadn’t written a Java generic method in a while, and I forgot you need to declare the generic type (<T>) early in the method declaration.

A Java Properties file method

In an earlier Java Properties file example I showed how to load a Java Properties file and then access the elements in the Properties object, but for today, if you just need a method to read a Java Properties file, I hope the following source code will be helpful to you:

Java: How to delete a directory tree

Java directory FAQ: "How do I delete a directory tree in Java?"

Java delete directory - discussion

The delete method of the File class won't delete a directory tree, meaning that in the old days you had to write your own recursive method to do this.

How to get tomorrow’s date in Java

Java Date FAQ: How do I determine tomorrow's date in Java?

Many times when you're working with a Java Date, you need to be able to add something to a date, i.e., to get tomorrow's date, or the next week, or the next year. In this short tutorial, we'll demonstrate how to easily add one day to today to get tomorrow's date.

A Java class that writes to and reads from a remote socket

I'm not going to describe this much today, but here's the source code for a Java class I put together from a number of other sources on the internet. In short, this code uses a Java Socket to connect to a port on a remote server, sends a command to that server to be executed, and then reads the output from the command that is executed. As a result, I assume that all information sent is text (nothing binary).

How do I convert a String to a long with Java?

Question: How do I convert a String to a long with Java?

Answer: The Long class includes a method named parseLong() that serves just this purpose. An example of a simple program that performs this conversion is shown below:

A Java keytool certificate example: Using ‘keytool’ with certificate files

Java keytool FAQ: Can you share an example of how to use the Java keytool command to create and share a Java/keytool certificate?

Here's a quick look at how two people, John and Paul, might use the Java keytool command to create and share a certificate file. In this example, John will create the certificate with the "keytool genkey" and "keytool export" commands, and Paul will import John's public key from the certificate file with the "keytool import" command.

Java 5 for loop syntax example (Java 5 Generics)

Java FAQ: Can you show me an example of the Java 5 for loop syntax? (Java 5 and newer)

Answer: Sure. I've created a sample Java program to demonstrate the Java 5 for-each loop syntax, using both a List with pre-Java5 syntax, and a second example using Java 5 generics syntax.

Java 5 for loop syntax example

Without any further ado, here is the example code:

Java and FTP: A Java class that lists FTP return codes (status codes)

I was just digging around through a Java FTP program I wrote, and found the following class, which might be a nice reference for other people. This class lists all the possible FTP server return codes (status codes) that your Java FTP program can receive in return to a call to an FTP server.

A Java MySQL DELETE example

Summary: A Java MySQL DELETE example, demonstrating how to issue a SQL DELETE command from your Java source code.

Java MySQL examples and tutorials

Today was mostly a day off for me, but I thought I'd do one thing here to help organize the website a little better. To that end, I've put together a list of the Java MySQL examples I've created, i.e., Java database examples that were specifically written for MySQL.