Class FileUtils

java.lang.Object
be.gaudry.model.file.FileUtils
All Implemented Interfaces:
PropertyChangeListener, EventListener

public class FileUtils extends Object implements PropertyChangeListener
Allows to manage files and paths. \n Interacts also with the operating system to provide some features like the name and the icon of a root file if it's a hard disk or a CDRom, etc. \n Provided by the broldev.core.model project.
Since:
1.0 Sep 10, 2008, broldev.core.model 0.0.1-SNAPSHOT dependency
Version:
1.3 Dec 5 2011
Author:
Steph GAUDRY
  • Field Details

    • LANGUAGE_PATH

      public static final String LANGUAGE_PATH
      Key to access to the language resource bundle
      See Also:
      Constant Field Values
    • iconCache

      protected static Map<String,​Icon> iconCache
      Icon cache to speed the rendering.
  • Method Details

    • finalize

      protected void finalize() throws Throwable
      Overrides:
      finalize in class Object
      Throws:
      Throwable
    • propertyChange

      public void propertyChange(PropertyChangeEvent evt)
      Localize this instance when the observed LanguageHelper is changed.
      Specified by:
      propertyChange in interface PropertyChangeListener
      Since:
      1.2 (Mar 30, 2009)
    • setLanguage

      protected void setLanguage()
      Localize some strings using the LanguageHelper features. See the logging information to know how the logging is implemented.
    • getLocaleStr

      public static String getLocaleStr(String key, String defaultStr)
      Loads the localized string matching the key if exists. If any problem occurs, return the defaultStr value.
      Parameters:
      key - to access to the localized string
      defaultStr - default value if a problem occurs
      Returns:
      localized string
    • getFileExtension

      public static String getFileExtension(String fileName)
      Returns a "Windows like" String with the file extension without the dot.
      Parameters:
      fileName - name of the file from wich we need the extension
      Returns:
      the extension, or an empty string on error
    • getFileExtension

      public static String getFileExtension(File file)
      Returns a "Windows like" String with the file extension without the dot.
      Parameters:
      file - from wich we need the extension
      Returns:
      the extension, or an empty string on error
    • getDirectoryLength

      public static long getDirectoryLength(AbstractBrolWorker bgw, File root, boolean includeSubFolders)
      Parses the sub directories to get the total size of a given directory.
      Parameters:
      bgw - SearchFilesWorker to cancel operation and report progress
      root - directory from wich we need the length
      includeSubFolders - add all sub directories informations (recursive method)
      Returns:
      length of the directory in bytes
    • getFilesCount

      public static int getFilesCount(AbstractBrolWorker bgw, File root, int counter)
      Parses the sub directories to get the total size of a given directory. \n TODO: internationalize the reported message
      Parameters:
      bgw - SearchFilesWorker to cancel operation and report progress
      root - directory from wich we need the length
      counter -
      Returns:
      count of files
    • open

      public static boolean open(String file)
      Launches the default application to open the file.

      If the specified file is a directory, the file manager of the current platform is launched to open it. On some platforms this feature may not be supported; in this case, the return value is false and the method call does nothing. See the logging information to know how the logging is implemented.

      Parameters:
      file - the file to be opened with the associated application
      Returns:
      true if the operation is supported by the platform
      Throws:
      NullPointerException - if file is null
      IllegalArgumentException - if the specified file dosen't exist
      UnsupportedOperationException - if the current platform does not support the open action
      SecurityException - if a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies read access to the file, or it denies the AWTPermission("showWindowWithoutWarningBanner") permission, or the calling thread is not allowed to create a subprocess
    • getMyDocumentsPath

      public static String getMyDocumentsPath()
      Provides a way to get the absolute path of the user's documents directory, because System.getProperty("user.home") gives the user directory and not the user documents directory. See the logging information to know how the logging is implemented.
      Returns:
      a File object representing the default starting folder
    • getApplicationPath

      public static String getApplicationPath()
      Returns:
      path of this file
      Since:
      1.2 (Feb 12 2009)
      See Also:
      getApplicationPath(Class, boolean)
    • getApplicationPath

      public static String getApplicationPath(Class<?> startingClass, boolean cached)
      System.getProperty("user.dir") does not return class directory each time. If we start a java application from a command prompt by giving the path (not using cd command to set the active directory), user.dir contains the active directory and not the application directory. See the logging information to know how the logging is implemented.
      Parameters:
      startingClass - class wich we need to know directory
      cached -
      • true to avoid doing the parsing each time. The result is stored as cache after the first call.
      • false to ignore cached result (this does not affect the cached result).
      Returns:
      path of the class arg
      Since:
      1.2 (Feb 12 2009)
    • deleteDirectory

      public static void deleteDirectory(Path directoryPath)
    • deleteDirectory

      public static boolean deleteDirectory(File file)
      Deprecated.
      Removes a directory from the files system.
      Parameters:
      file - file to delete
      Returns:
      true if the file has been deleted
    • getFileChooser

      protected static JFileChooser getFileChooser()
    • getFileSystemView

      static FileSystemView getFileSystemView()
    • getFileModificationDateString

      public static String getFileModificationDateString(File file)
      Returns a localized string (never null) with the time that the file was last modified or a short localized message on error. See the debug log for more information about the error. \n Be careful than some differences may be found depending on the platform.
      Parameters:
      file - the file that we want the last modification date
      Returns:
      last modification date of the file, or a localized error message
    • getFileModificationDate

      public static Date getFileModificationDate(File file)
      Returns the time that the file was last modified. \n BeSee the logging information to know how the logging is implemented.latform. See the logging See the logging information to know how the logging is implemented.fication date
      Parameters:
      file - the file that we want the last modification date
      Returns:
      the last modification date of a file, or null on error
    • getSystemTypeDescription

      public static String getSystemTypeDescription(File file)
      Returns a string (never null) with a platform dependent description of the file or a localized error message (file not found, etc).
      Parameters:
      file - wich we want the description
      Returns:
      the file description or the error message
    • getSystemDisplayName

      public static String getSystemDisplayName(File file)
      Returns a string (never null) with the platform dependent name of the file or a localized error message (file not found, etc). This is mostly useful for special system files like an IO device or user's documents directory, etc.
      Parameters:
      file - wich we want the system name
      Returns:
      the system name of the file or the error message
      See Also:
      getSystemDisplayName(File, boolean)
    • getSystemDisplayName

      public static String getSystemDisplayName(File file, boolean rootFile)
      Returns a string (never null) with the platform dependent name of the file or a localized error message (file not found, etc). This is mostly useful for special system files like an IO device or user's documents directory, etc. Does not output into the log on error to improve performances on iterated call of this method (parsing the system files).
      Parameters:
      file - wich we want the system name
      rootFile -
      Returns:
      the system name of the file or the error message
    • getFileSizeString

      public static String getFileSizeString(File file)
      Returns a localized string (never null) with the size of the file or a localized error message (file not found, etc). Does not output into the log on error to improve performances on iterated call of this method (parsing the system files).
      Parameters:
      file - wich we want the size
      Returns:
      the string of the size or a localized message
      See Also:
      UnitUtils.getLengthString(long)
    • getDesktopFolder

      public static File getDesktopFolder()
      Returns the user's desktop folder.
      Returns:
      the desktop folder
    • getDesktopName

      public static String getDesktopName()
      Returns the user's desktop folder localized name.
      Returns:
      the desktop name
    • getMyComputerName

      public static String getMyComputerName()
      Returns a localized version of the "Windows like" string of "My computer". "My Computer" is not a real file, but a kind of root of the storage devices.
      Returns:
      the localized string
    • getMyComputerIcon

      public static Icon getMyComputerIcon()
      Returns the platform version of an icon for the Use a cache to store system icons icon.
      Returns:
      System Icon of my computer
      See Also:
      getMyComputerName()
    • getSystemIcon

      public static Icon getSystemIcon(File file)
      Returns the system dependent icon for a given file. Never returns null :
      • if the file is null or not exists, return a new MissingIcon.
      • returns a system dependent default file or folder icon on error
      Use a cache mechanism to store system icons by file extension. Exe files and folders are not cached. \n TODO: check linux default icons (can't use gnome themes?) \n TODO: don't use cache for images files, use thumbnails???
      Parameters:
      file - wich we want the icon
      Returns:
      System Icon of a given file
    • getGenericFileIcon

      public static Icon getGenericFileIcon()
      Returns a system dependent icon for the generic files
      Returns:
      the generic file icon
    • getGenericFolderIcon

      public static Icon getGenericFolderIcon()
      Returns a system dependent icon for the generic folders The system dependent icons for customized folders are available by calling the getSystemIcon(File) method
      Returns:
      the generic folder icon
    • copyAndReplace

      public static boolean copyAndReplace(File source, File target)
      Copy a file from a source to a target. \n If the parent folder of the target doesn't exist, the target structure is created. The target file is replaced if already exists.
      Parameters:
      source - file to copy
      target - copied file
      Returns:
      true if the file is copied (a warning is also sent to the logger in case of failure)
      Since:
      1.3 (Dec 5, 2011)
    • copy

      @Deprecated public static boolean copy(File source, File target)
      Deprecated.
      1.3 (Dec 5, 2011) see copyAndReplace(File, File)
      Copy a file from a source to a target. \n If the parent folder of the target doesn't exist, the target structure is created. The target file is replaced if already exists.
      Parameters:
      source - file to copy
      target - copied file
      Returns:
      true if the file is copied (a warning is also sent to the logger in case of failure)
    • getIncrementedFile

      public static File getIncrementedFile(File file)