Create Math formula image

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:

Representation of a Math formula

Continue reading “Create Math formula image”

Traversing the file system

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:

 

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:

Illustrating traversing the file directory structure depth first.

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.

Illustrating traversing the file directory structure breadth first.

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.

ANT – A worked example

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.

Our project structure looks like this:

The sample Ant project structure

We are going to use ANT to produce a Windows executable application.  The steps we need to take to do this are: – Continue reading “ANT – A worked example”

Having learnt the language now what?

Lots of people set out to learn a language, take a course and then at the end of it don’t know what to do next. They don’t feel they are a coder, they still can’t do anything of substance. We all go through this. There is a chasm between knowing a programming language and being able to do things with it. The next step is to learn to write an application. Continue reading “Having learnt the language now what?”