From Wikipedia, the free encyclopedia
Jump to navigation Jump to search

Developer(s)Keith Bishop
Stable release
1.0-alpha / April 29, 2008 (2008-04-29)
Written inJava (programming language)

FSPath is a lightweight API written in Java. The goal of FSPath is to make interacting with the file system simpler. It was first released in April 2008 by Keith Bishop.

FSPath is licensed under the GNU General Public License and is free and open source software.


The notable features of the FSPath API are

  1. A query language for defining file system queries (Based on XPath).
  2. A fluent interface[1] to enable the chaining of different file system operations such as copying, renaming and deleting.
  3. A plug-in mechanism for developers to implement custom file system operations by use of callbacks.


When used in conjunction, these two features provide the software developer with a great deal of power and flexibility in return for very few keystrokes.

The example below shows a typical recursive search algorithm implemented in Java, for searching for a file by name.

 public List<File> listFiles(File currentDirectory) {

    List<File> fileList = new ArrayList<File>();

    File[] files = currentDirectory.getFiles();

    for(int i = 0; i < files.length; i++) {

        if (file[i].isDirectory()) {

        } else if (file[i].getName().endsWith(".txt")) {


    return fileList;


The code above can be substituted with the following FSPath API calls...

  FSPath fspath = FSPathFactory.newFSPath();

  FSPathResultList results = fspath.query("//file[contains(@name, '.txt')]");

FSPath's fluent interface enables simple manipulation of files by 'chaining' the desired file system operation onto the search query...

  FSPath fspath = FSPathFactory.newFSPath();

  FSPathResultList results = fspath.query("//file[contains(@name, '.txt')]").delete();

To perform custom behaviour on a file or set of files, the developer can implement the net.sf.sugar.fspath.Callback interface. The example below shows an anonymous inner class being used as a callback.

        .each(new Callback() {
                      public void call(FSPathResult result) {
                          File file = result.getFile();
                          System.out.println(file.getName() + " has : "
                          + file.listFiles().size() + " children");

Further reading[edit]


  1. ^ "Fluent interface". Retrieved 2008-11-18.