Package rs.baselib.io

Class FileFinder

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

public class FileFinder
extends java.lang.Object
Utility to find a file on disk.
Author:
RalphSchuster
See Also:
find(Class, String)
  • Constructor Summary

    Constructors 
    Constructor Description
    FileFinder()  
  • Method Summary

    Modifier and Type Method Description
    static java.net.URL find​(java.lang.Class<?> clazz, java.lang.String name)
    Tries to find the file specified from filesystem or classpath.
    static java.net.URL find​(java.lang.String name)
    Tries to find the file specified from filesystem or classpath.
    static java.lang.String load​(java.lang.Class<?> clazz, java.lang.String name)
    Finds and loads a file.
    static java.lang.String load​(java.lang.String name)
    Finds and loads a file.
    static java.io.InputStream open​(java.lang.Class<?> clazz, java.lang.String name)
    Tries to find the file specified from filesystem or classpath.
    static java.io.InputStream open​(java.lang.String name)
    Tries to find the file specified from filesystem or classpath.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • find

      public static java.net.URL find​(java.lang.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:
      find(Class, String)
    • find

      public static java.net.URL find​(java.lang.Class<?> clazz, java.lang.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
    • open

      public static java.io.InputStream open​(java.lang.String name) throws java.io.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:
      java.io.IOException - when file cannot be opened
    • open

      public static java.io.InputStream open​(java.lang.Class<?> clazz, java.lang.String name) throws java.io.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:
      java.io.IOException - when file cannot be opened
      See Also:
      find(Class, String)
    • load

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

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