How to Use a FileFilter in Java to List Files

In this example we use a FileFilter to list files of a certain type. To implement the FileFilter interface we need to create an accept() method which takes a java.io.File object as parameter.
In the method we check whether the name ends with “.txt” and if so we return true, else false.
When we have created the FileFilter we can pass it to the listFiles method of the java.io.File class which will use it to return an array of all files that the filter returned true for in its accept method.
In the example we check the C:\temp folder for files ending with the .txt extension.

Continue reading

Read Data from InputStream to a String

This code example shows how to read data from an InputStream object and ultimately store it in a String object.
To read data from our file we use the method getResourceAsStream() which we get from our class object.
The class object is retrieved by calling getClass() on our Main class.
The method getResourceAsStream() enables us to read a file located within the same jar-file as the actual program, in case it is packaged as such.
It can also be used to read from the root directory of the application as done in the example by adding a slash before the filename.

Continue reading

Using BufferedReader to read input number from user

This example shows how to read input from console using the BufferedReader class. The BufferedReader has a neat method called readLine() that reads all the input characters into a string when the user hits the Enter button instead of reading byte by byte.
This example takes the input string and parses it into a variable of type Double and then prints out the square root of that number.
This is the code:

Continue reading

Redirect standard error

To redirect standard error (or System.err) there’s a method in the System class called setErr() which takes a PrintStream instance as argument.
If we for example want to redirect all messages on System.err to a file, we can create a FileOutputStream instance and send it to the constructor of the PrintStream.
This Java code example creates the FileOutputStream with the filename “standard_err.txt” and pass it to the PrintStream, which in turn is passed to the setErr() method.
A NullPointerException is forced to test that the stacktrace is printed to the file.

Continue reading

Redirect standard output

If you want to change the location to where the standard output messages go there’s a method in the System class called setOut() which takes a PrintStream instance as argument.
In this code example we show how to redirect all standard output messages to a file by creating a FileOutputStream instance and pass it to the PrintStream instance that is sent to the setOut method, like so:

Continue reading

Loading configuration parameters from textfile into a program

Loading parameters into a program is easily handled in Java through the Properties class.
The only thing that has to be done is to create an instance of the Properties class and one instance of
FileInputStream class that points to the configuration file.
The actual loading of the parameters are done through the method load() on the Properties object.
In the example below parameters are read from the configuration file and then printed out.

Continue reading