Interface FileFinder

All Known Subinterfaces:
StaticFiles
All Known Implementing Classes:
BasicFileFinder, BasicStaticFiles, FileFinder.Void, StaticFiles.Void

public interface FileFinder
Utility class for finding regular or localized files on the classpath and file system.
Example:

// Constructor a file source that looks for files in the "files" working directory, then in the // package "foo.bar", then in the package "foo.bar.files", then in the package "files". FileFinder finder = FileFinder .create() .dir("files") .cp(foo.bar.MyClass.class,null,true) .cp(foo.bar.MyClass.class,"files",true) .cp(foo.bar.MyClass.class,"/files",true) .cache(1_000_000l) // Cache files less than 1MB in size. .ignore(Pattern.compile("(?i)(.*\\.(class|properties))|(package.html)")) // Ignore certain files. .build(); // Find a normal file. InputStream is1 = finder.getStream("text.txt"); // Find a localized file called "text_ja_JP.txt". InputStream is2 = finder.getStream("text.txt", Locale.JAPAN);

If the locale is specified, then we look for resources whose name matches that locale. For example, if looking for the resource "MyResource.txt" for the Japanese locale, we will look for files in the following order:

  1. "MyResource_ja_JP.txt"
  2. "MyResource_ja.txt"
  3. "MyResource.txt"

The default implementation of this interface is BasicFileFinder. The FileFinder.Builder.type(Class) method is provided for instantiating other instances.

Example:

public class MyFileFinder extends BasicFileFinder { public MyFileFinder(FileFinder.Builder builder) { super(builder); } } // Instantiate subclass. FileFinder myFinder = FileFinder.create().type(MyFileFinder.class).build();

Subclasses must provide a public constructor that takes in any of the following arguments:

  • FileFinder.Builder - The builder object.
  • Any beans present in the bean store passed into the constructor.
  • Any Optional beans optionally present in bean store passed into the constructor.
See Also:
  • Method Details

    • create

      static FileFinder.Builder create(BeanStore beanStore)
      Static creator.
      Parameters:
      beanStore - The bean store to use for creating beans.
      Returns:
      A new builder for this object.
    • create

      Static creator.
      Returns:
      A new builder for this object.
    • getStream

      Returns the contents of the resource with the specified name.
      Parameters:
      name - The resource name. See Class.getResource(String) for format.
      locale - The locale of the resource to retrieve.
      If null, won't look for localized file names.
      Returns:
      The resolved resource contents, or null if the resource was not found.
      Throws:
      IOException - Thrown by underlying stream.
    • getString

      Returns the file with the specified name as a string.
      Parameters:
      name - The file name.
      locale - The locale of the resource to retrieve.
      If null, won't look for localized file names.
      Returns:
      The contents of the file as a string. Assumes UTF-8 encoding.
      Throws:
      IOException - If file could not be read.