Posts in the “java” category

How to determine the directory your Java application was started in (user.dir)

If you ever need to determine what directory your Java code is being run from (essentially the current working directory), you can get this information from the system properties, specifically the System.getProperty or System.getProperties methods.

The following line of Java code shows how to determine what directory your Java application was started in. This information is stored in the user.dir system property, which you access like this:

How to append text to a file with the Java FileWriter class

Java file writing FAQ: How do I append text to the end of a text file in Java?

The short answer is that you should create a FileWriter instance with the append flag set to true, like this:

BufferedWriter bw = new BufferedWriter(new FileWriter("checkbook.dat", true));

The rest of this article explains this.

How to create directories in Java

Java directory FAQ: How do I create a directory (or directories) in Java? (Also written as, "How do I make a directory in Java?)

To create a directory in Java, just use the "mkdir" or "mkdirs" methods of the Java File class. Here are a few examples to demonstrate this.

How to read a Java Properties file

Java Properties file FAQ: Can you show me how to read a Java Properties file?

Here is a snippet of Java source code that shows how to read a Java Properties file named Pizza.properties. It reads in two properties named CRUST and TOPPINGS.:

How to disassemble Java code with ‘javap -c’

One of my favorite Java subjects is code optimization and performance. Here I'd like to show you a couple of neat things you can learn with the javap -c command. This command lets you disassemble Java bytecode.

The first thing you need to have for this exercise is a little sample Java code. So in the examples below I create two test Java classes, appropriately named Test1.java and Test2.java. Although it's not explicitly stated below, the steps I'm going to follow are these:

Java: How to list the files in a directory

Java file directory list FAQ: How do I create a list of files in a directory using Java?

Here's a quick example of a Java class that demonstrates how to create a list of all files in a directory using just the Java File class. Specifically, this example shows how to list all the files in a directory, store those filenames in a String array, and then print the array.

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.

What is a Java NumberFormatException?

Java exception FAQ: What is a Java NumberFormatException?

Answer: A Java NumberFormatException usually occurs when you try to do something like convert a String to a numeric value, like an int, float, double, long, etc.

The best way to show a NumberFormatException is by example, so here’s an example where I intentionally write bad Java code to throw a NumberFormatException:

How to create and throw a custom exception in Java

Java exceptions FAQ: How do I create a custom exception in Java?

Here's a quick example of how to create and throw a custom exception class in Java. In this tutorial I'll demonstrate how to (1) create a custom exception class in Java; (2) throw our custom Java exception; (3) catch our custom exception; and (4) look at the output from our custom exception when we print a stack trace.

[toc hidden:1]

Java “file exists” testing

Java "file exists" FAQ: How can I test to see if a file or directory exists in Java?

Java File exists method

To test to see if a file or directory exists, use the "exists" method of the Java File class, as shown here:

File varTmpDir = new File("/var/tmp");
boolean exists = varTmpDir.exists();

The Java File exists method returns true if the file or directory exists, and false otherwise.

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.

A `printf` format reference page (cheat sheet)

Summary: This page is a printf formatting cheat sheet. I originally created this cheat sheet for my own purposes, and then thought I would share it here.

A great thing about the printf formatting syntax is that the format specifiers you can use are very similar — if not identical — between different languages, including C, C++, Java, Perl, PHP, Ruby, Scala, and others. This means that your printf knowledge is reusable, which is a good thing.