The other day I came across an example where a formula written in a Java program could be represented as a standard equation on the screen. I thought this was brilliant. I recall back in my Uni days using the formula editor in Microsoft Word to present formulas. Now I don’t have a use for this in Java yet but, you never know. However, in case anyone out there does have a requirement to produce a formula from within their Java program then maybe this will help.
The challenge is to produce an output that looks like this:
Traversing the file system is one of the common tasks required in applications. Search the web for examples and you can find plenty of examples of how to go about it. One of the most popular is to work through the file system using recursive calls like this:
* @author jmcneil
* (c) copyright Software Pulse 2018
* @param args
showFiles(file.listFiles());// Calls same method again.
Now the beauty of this code is that it is clean compact and elegant but not without issue.
While the recursive call solution gets the job done some feel that the recursive nature of the solution can be problematic. Repeatedly calling the same function can blow the stack, so if you have a large file system to work through there is the possibility your application will crashing.
That said, for most applications that is not an issue, so if you are looking for something that can work through a file system then the recursive call solution will get the job done.
Now there was a reason I was looking at working through a file system. That reason was that I was building an application where I wanted to walk through a file system. However, the solution presented earlier did not meet my needs. You see the recursive call solution navigates the file system depth first. What that means is given the following directory structure:
The code would process files in the directories in the order numbered from 1 to 7. As you can see the recursive call method works its way down the structure and then across.
My particular requirement was to process all the files in the top level directory, then all the files in the directories below the top directory before moving on to the directories below them. So this is the order in which I wanted to process the directories.
The solution was based on the recursive call solution but ironically the changes resulted in a non-recursive call.
This new solution is not as elegant, compact or I suspect as fast however, it does deliver the solution I was looking for. The general approach is this:
Given a starting point within a file system, say the root directory, make a File object from the String containing the path and add this to an ArrayList object. Then set up a FOR loop to iterate through all the items in the ArrayList. Initially this will only be the single item we have added. A check is made to see if the File object is a directory and again for the first item this should be true. If not true then we go on to process the next item in the ArrayList but if true a call is made to a method called showFiles and the File object is passed as a parameter. The showFile method will return an array of Files all of which will be file system directories. These directories are added to the ArrayList and the FOR loop upper limit is adjusted to take into account the addition of extra items.
The showFiles method is where all the files in the current directory are identified and processed. Now in my case I’m only interested in image files so I use a Filter class to obtain only those files of the type I’m interested in. Another FOR loop processes each of these files in turn.
Once all the files are processed, I then use an inner class to create a directory filter to allow all the directories within the current directory to be returned. This is then returned to the invoking method.
One item of note is that there are instances where a Null value may be return. This would need to be handled to prevent an error in the main method. The code for the WalkDirectory2 class looks like this:
The FileNameFilterOption class implements FilternameFilter and gives an example of how to filter for specific file types. Note that if the operating system is case sensitive you will need to handle this. As you can see from the code I have simply added both upper and lower case to the filter. This is not an ideal solution but demonstrates the point. A more elegant solution would be to allow the filter to be set using a properties file or if there is no requirement to have a static implementation then add a method to set a filter property.
So the code for FilenameFilerOptions look like this:
And that’s it, a way of traversing through the file system and processing all the files in order of how deep they are from the starting point. Also an example of how to use filtering to process only those files or interest.
If you would like to download the source code for this example click here (email address required)
One final point, with the introduction of Java version 8 and perhaps even version 7 there are now alternative ways to navigate through the file system, I have gone with this method as I am supporting Java version 6 and above.
Whilst developing an application the other day I wanted to create a repeating task. Looking around for a solution I came across the Timer and TimerTask classes. These two classes in combination looked like a perfect solution to my problem. It was only when I came to implement my solution I noticed something about how they work. Continue reading “Using Java Timer to build a countdown timer”
Imagine we have a small JavaFX application. We are building this application in Eclipse, although this should not make any material difference, you could build it in using any IDE or simple command line tools.
This application has some java files under the “src” folder. There is also a “resources” folder which contains some none java files which the application will reference. Added to this we are using some 3rd party JAR libraries. Finally, we have a text-based properties file for our application.
When you write an application you set out to make it work. However, quite often once an application is deployed in the field you can encounter problems. Having a log file that can help you trace where the problem lies can be an invaluable tool. Continue reading “Configure Log4J2 with properties file”
Using a text file to hold configurable values for your application is a great way to provide flexibility without needing to re-compile your code. This tutorial takes you through how to add a properties file to your Java desktop application.
If your java desktop application needs to store data in a database then a good solution is to embed a database into your solution. This tutorial takes you step by step through embedding an SQLite database into your Java desktop application. Continue reading “Java with SQLite embedded database”
JTree is a swing class used to display a tree like structure such as the file directory structure. If we want to create a JTree we could simply call any one of a number of constructors and it would give us a JTree. To demonstrate the point Continue reading “Getting started with JTree”