Package rs.baselib.io

Class FileFinder

java.lang.Object
rs.baselib.io.FileFinder

public class FileFinder extends Object
Utility to find a file on disk.
Author:
RalphSchuster
See Also:
  • Constructor Details

    • FileFinder

      public FileFinder()
  • Method Details

    • find

      public static URL find(String name)
      Tries to find the file specified from filesystem or classpath.
      Parameters:
      name - - name of file, can be fully qualified
      Returns:
      URL to the file
      See Also:
    • findDir

      public static URL findDir(String name)
      Tries to find the directory specified from filesystem or classpath.
      Parameters:
      name - - name of directory, can be fully qualified
      Returns:
      URL to the directory
      See Also:
    • find

      public static URL find(Class<?> clazz, String name)
      Tries to find the file specified from filesystem or classpath.

      The file will be searched based on the following procedure:

      • Try to find the file in current working dir (unless absolute path is given).
      • Try to fine the file in package of the class given as argument using the default class loader
      • Try to find the file in parent packages of the class given as argument using the default class loader
      • Try to fine the file in package of the class given as argument using the context class loader
      • Try to find the file in parent packages of the class given as argument using the context class loader
      • Repeat the procedure by trying to find the file with a prepended slash.
      Parameters:
      name - - name of file, can be fully qualified
      clazz - - class to get the class loader from
      Returns:
      URL to the file
    • findDir

      public static URL findDir(Class<?> clazz, String name)
      Tries to find the directory specified from filesystem or classpath.

      The directory will be searched based on the following procedure:

      • Try to find the dir in current working dir (unless absolute path is given).
      • Try to fine the dir in package of the class given as argument using the default class loader
      • Try to find the dir in parent packages of the class given as argument using the default class loader
      • Try to fine the dir in package of the class given as argument using the context class loader
      • Try to find the dir in parent packages of the class given as argument using the context class loader
      • Repeat the procedure by trying to find the dir with a prepended slash.
      Parameters:
      name - - name of dir, can be fully qualified
      clazz - - class to get the class loader from
      Returns:
      URL to the dir
    • find

      public static URL find(Class<?> clazz, String name, boolean findFiles)
      Tries to find the file or directory specified from filesystem or classpath.

      The file/directory will be searched based on the following procedure:

      • Try to find the file/dir in current working dir (unless absolute path is given).
      • Try to fine the file/dir in package of the class given as argument using the default class loader
      • Try to find the file/dir in parent packages of the class given as argument using the default class loader
      • Try to fine the file/dir in package of the class given as argument using the context class loader
      • Try to find the file/dir in parent packages of the class given as argument using the context class loader
      • Repeat the procedure by trying to find the file/dir with a prepended slash.
      Parameters:
      name - - name of file or directory, can be fully qualified
      clazz - - class to get the class loader from
      findFiles - - true when files shall be found, false when directories are to be found
      Returns:
      URL to the file or directory
    • open

      public static InputStream open(String name) throws IOException
      Tries to find the file specified from filesystem or classpath.
      Parameters:
      name - - name of file, can be fully qualified
      Returns:
      stream to the file
      Throws:
      IOException - when file cannot be opened
    • open

      public static InputStream open(Class<?> clazz, String name) throws IOException
      Tries to find the file specified from filesystem or classpath.
      Parameters:
      name - - name of file, can be fully qualified
      clazz - class to get the class loader from
      Returns:
      stream to the file
      Throws:
      IOException - when file cannot be opened
      See Also:
    • load

      public static String load(String name) throws IOException
      Finds and loads a file.
      Parameters:
      name - - name of file
      Returns:
      contents of file
      Throws:
      IOException - when file cannot be opened
      See Also:
    • load

      public static String load(Class<?> clazz, String name) throws IOException
      Finds and loads a file.
      Parameters:
      name - - name of file
      clazz - class to get the class loader from
      Returns:
      contents of file
      Throws:
      IOException - when file cannot be opened
      See Also: