Class FileUtils

java.lang.Object
imagingbook.core.FileUtils

public abstract class FileUtils extends Object
This class defines various static methods for managing file-based resources and JAR manifest files.
  • Constructor Details

  • Method Details

    • stripFileExtension

      public static String stripFileExtension(String name)
      Removes the extension part of a pathname. Examples:
      "foo.txt" → "foo", "foo" → "foo", "foo." → "foo.", ".txt" → ".txt".
      Parameters:
      name - the pathname
      Returns:
      the pathname without the extension (if valid)
    • getFileExtension

      public static String getFileExtension(String name)
      Extracts the extension part of a pathname as a string. Examples:
      "foo.txt" → "txt", "foo" → "", "foo." → "", ".txt" → "".
      Parameters:
      name - the pathname
      Returns:
      the extension or an empty string
    • getClassPath

      public static String getClassPath(Class<?> clazz)
      Find the path from which a given class was loaded.
      Parameters:
      clazz - a class.
      Returns:
      the path of the .class file for the given class or null (e.g. if the class is anonymous).
    • printClassPath

      public static void printClassPath()
      Lists (to System.out) the paths where classes are loaded from.
    • checkClass

      public static boolean checkClass(String classname)
      Checks 'by name' if a particular class exists.
      Parameters:
      classname - fully qualified name of the class, e.g. imagingbook.lib.util.FileUtils
      Returns:
      true if the class was found, false otherwise
    • copyToFile

      public static void copyToFile(InputStream in, File file) throws IOException
      Reads all data from the given input stream and copies them to to a file.
      Parameters:
      in - the input stream
      file - the output file
      Throws:
      IOException - if anything goes wrong
    • getJarManifest

      public static Manifest getJarManifest(Class<?> clazz)
      Finds the manifest (from META-INF/MANIFEST.MF) of the JAR file from which clazz was loaded. See: http://stackoverflow.com/a/1273432
      Parameters:
      clazz - A class in the JAR file of interest.
      Returns:
      A Manifest object or null if clazz was not loaded from a JAR file.
    • selectFolder

      public static File selectFolder(String startDirectory, String dialogTitle)
      Opens a dialog for the user to select a single folder (no files). Contained files and sub-folders are shown. Uses native (system) look-and-feel; original look-and-feel is restored.
      Parameters:
      startDirectory - the directory to start from (pass "" or "." for the current directory)
      dialogTitle - the string shown in the title bar of the dialog window
      Returns:
      a File object representing the selected directory or null if the dialog was canceled
    • setCurrentDirectory

      @Deprecated public static void setCurrentDirectory(String pathname)
      Deprecated.
      Sets a system property to memorize the current directory. Use getCurrentDirectory() to retrieve this value. If the specified pathname is not a directory (i.e., a plain file), its parent directory is used.
      Parameters:
      pathname - a directory of file path
    • setCurrentDirectory

      public static void setCurrentDirectory(Class<?> clazz, String pathname)
      Associates a current directory with the specified class by setting a system property to make this persistent through class reloads. Use getCurrentDirectory(Class) to retrieve this value. If the specified pathname is not a directory (i.e., a plain file), its parent directory is used.
      Parameters:
      clazz - the class to associate the directory with
      pathname - a directory of file path
    • setCurrentDirectory

      public static void setCurrentDirectory(Class<?> clazz, Path path)
    • getCurrentDirectory

      Deprecated.
      Returns the current directory path. If the directory was set with setCurrentDirectory(String) before this path is returned, otherwise the value for the "user.dir" system property.
      Returns:
      a string with the current directory path
    • getCurrentDirectory

      public static String getCurrentDirectory(Class<?> clazz)
      Returns the current directory associated with the specified class. (usually of type PlugIn or PlugInFilter). If the directory was set with setCurrentDirectory(Class,String) before this path is returned, null otherwise.
      Parameters:
      clazz - the class to associate the directory with
      Returns:
      a string with the current directory path or null if not registered
    • getTempDirectory

      public static String getTempDirectory()
    • getModuleName

      public static String getModuleName(Class<?> clazz)
    • getModuleName2

      public static String getModuleName2(Class<?> clazz) throws URISyntaxException
      Throws:
      URISyntaxException
    • main

      public static void main(String[] args) throws URISyntaxException
      Throws:
      URISyntaxException