public class

FileFinder

extends Object
java.lang.Object
   ↳ com.microstrategy.utils.FileFinder

Class Overview

Helper class for locating files. Read and write to them without worrying about the precise file path as long as the file is in the classpath. If the full path is specified, then it will be used regardless of whether the file is in the classpath or not.

Note: the ClassLoader used to locate the resource is the same one used to load the FileFinder class, which is not necessarily the system ClassLoader (e.g. servlet engines often have custom ClassLoaders).

Summary

Constants
int FLAG_FILE
int FLAG_READABLE
int FLAG_WRITABLE
Fields
public static final String PATH_SEPARATOR
Public Constructors
FileFinder()
Public Methods
static void check(String filePath)
static void check(String filePath, int flags)
Validates file attributes.
static boolean checkIfValid(String filePath, int flags)
Same as check(String, int), except this method returns true or false as to whether the validation succeeds instead of throwing exceptions.
static String findDir(String path)
Locates a resource either from the filepath, or classpath, and returns its encapsulating directory.
static String findDir(String path, ClassLoader cl)
Locates a resource either from the filepath, or classpath, and returns its encapsulating directory.
static String findPath(String path, ClassLoader cl)
Locates a resource either from the filepath, or classpath.
static String findPath(String path)
Locates a resource either from the filepath, or classpath.
static String findPathJar(String path, ClassLoader cl)
Locates a resource either from the filepath, or classpath.
static String findPathJar(String path)
Locates a resource either from the filepath, or classpath.
static URL findResource(String name, ClassLoader cl)
Finds resource with given name.
static URL findResource(String name)
Finds resource with given name.
static InputStream getResourceAsStream(String resourcePath, ClassLoader cl)
Attempts to locate resource from the given file path.
static InputStream getResourceAsStream(String resourcePath)
Attempts to locate resource from the given file path.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int FLAG_FILE

Constant Value: 4 (0x00000004)

public static final int FLAG_READABLE

Constant Value: 1 (0x00000001)

public static final int FLAG_WRITABLE

Constant Value: 2 (0x00000002)

Fields

public static final String PATH_SEPARATOR

Public Constructors

public FileFinder ()

Public Methods

public static void check (String filePath)

Parameters
filePath file path
Throws
FileNotFoundException if filePath does not exist
IOException if check based on given flags fail

public static void check (String filePath, int flags)

Validates file attributes. This operation will only work for file paths in the file system. Available flags: FLAG_FILE, FLAG_READABLE, FLAG_WRITABLE.

Parameters
filePath file path
flags set to check for read, write or file/directory validation
Throws
FileNotFoundException if filePath does not exist
IOException if check based on given flags fail

public static boolean checkIfValid (String filePath, int flags)

Same as check(String, int), except this method returns true or false as to whether the validation succeeds instead of throwing exceptions.

Parameters
filePath The file path.
flags Set to check for read, write or file/directory validation.
Returns
  • whether validation succeeds or fails

public static String findDir (String path)

Locates a resource either from the filepath, or classpath, and returns its encapsulating directory. Note that this method will NOT return files inside a JAR.

Parameters
path resource file name or resource file path - it should either be in the classpath, or be specified by a fully qualified path.
Returns
  • The canonical path of the encapsulating directory of the resource file.
Throws
FileNotFoundException if the path does not exist
IOException if parent directory cannot be obtained from given path.

public static String findDir (String path, ClassLoader cl)

Locates a resource either from the filepath, or classpath, and returns its encapsulating directory. Note that this method will NOT return files inside a JAR.

Parameters
path resource file name or resource file path - it should either be in the classpath, or be specified by a fully qualified path.
cl The ClassLoader to use to find objects in the class path.
Returns
  • The canonical path of the encapsulating directory of the resource file.
Throws
FileNotFoundException if the path does not exist
IOException if parent directory cannot be obtained from given path.

public static String findPath (String path, ClassLoader cl)

Locates a resource either from the filepath, or classpath. A FileNotFoundException is thrown if it cannot be found. Note that this method will NOT return files located in a JAR. An IllegalArgumentException is thrown if the path is empty. This method is useful in obtaining paths in the file system. To find paths that may be inside a JAR, see findPathJar(String).

Parameters
path file name or file path - it should either be in the classpath, or be specified by a fully qualified path.
cl The ClassLoader to use to find objects in the class path.
Returns
  • URL path of the file.
Throws
IllegalArgumentException if path is empty
FileNotFoundException if the file does not exist
IOException if a problem occurs while deriving the path URL

public static String findPath (String path)

Locates a resource either from the filepath, or classpath. A FileNotFoundException is thrown if it cannot be found. Note that this method will NOT return files located in a JAR. An IllegalArgumentException is thrown if the path is empty. This method is useful in obtaining paths in the file system. To find paths that may be inside a JAR, see findPathJar(String).

Parameters
path file name or file path - it should either be in the classpath, or be specified by a fully qualified path.
Returns
  • URL path of the file.
Throws
IllegalArgumentException if path is empty
FileNotFoundException if the file does not exist
IOException if a problem occurs while deriving the path URL

public static String findPathJar (String path, ClassLoader cl)

Locates a resource either from the filepath, or classpath. A FileNotFoundException is thrown if it cannot be found. Note that this method will return paths located in JARs on the classpath. To find paths excluding those in a JAR, see findPath(String).

Parameters
path either a fully qualified file path, or a relative resource path
cl The ClassLoader to use to find objects in the class path.
Returns
  • URL path of the file
Throws
IllegalArgumentException if path is empty
FileNotFoundException if path does not exist
IOException if a problem occurs while deriving the path URL

public static String findPathJar (String path)

Locates a resource either from the filepath, or classpath. A FileNotFoundException is thrown if it cannot be found. Note that this method will return paths located in JARs on the classpath. To find paths excluding those in a JAR, see findPath(String).

Parameters
path either a fully qualified file path, or a relative resource path
Returns
  • URL path of the file
Throws
IllegalArgumentException if path is empty
FileNotFoundException if path does not exist
IOException if a problem occurs while deriving the path URL

public static URL findResource (String name, ClassLoader cl)

Finds resource with given name.

Parameters
name resource name.
cl The ClassLoader to use to find objects in the class path.
Returns
  • URL of resource

public static URL findResource (String name)

Finds resource with given name.

Parameters
name resource name.
Returns
  • URL of resource

public static InputStream getResourceAsStream (String resourcePath, ClassLoader cl)

Attempts to locate resource from the given file path. If not found, it will try using the application ClassLoader to locate the resource.

Parameters
resourcePath File path for the resource.
cl The ClassLoader to use to find objects in the class path.
Returns
  • The InputStream of resource file.
Throws
FileNotFoundException Thrown when resource file can not be found.
IOException Thrown if a problem occurs while deriving the path URL.

public static InputStream getResourceAsStream (String resourcePath)

Attempts to locate resource from the given file path. If not found, it will try using the application ClassLoader to locate the resource.

Parameters
resourcePath File path for the resource.
Returns
  • The InputStream of resource file.
Throws
FileNotFoundException Thrown when resource file can not be found.
IOException Thrown if a problem occurs while deriving the path URL.