Class FileUtils

java.lang.Object
com.nomagic.magicreport.helper.FileUtils

@OpenApiAll public final class FileUtils extends Object
A collection of utility methods for File handler.
Since:
1.0 Nov 20, 2006
Version:
May 17, 2007
  • Method Details

    • createNonOverwriteFile

      public static File createNonOverwriteFile(File parent, String prefix, String suffix)
      Create a non-overwrite file from given name and extension. The format of new file name is name + "." + extension. If file already exists, append (number) after name.

      
       Example: createNonOverwriteFile(parent, "filename", "txt");
       // result of this method is "filename.txt"
       // if "filename.txt" already exists, the new file name is "filename(1).txt"
       
      Parameters:
      parent - The directory in which the file is to be created
      prefix - The prefix string to be used in generating the file's name
      suffix - The suffix string to be used in generating the file's name; may be extension of file.
      Returns:
      an instance of File
    • createNonOverwriteFile

      public static File createNonOverwriteFile(File parent, String child)
      Create a non-overwrite file from given name and extension. The format of new file name is name + "." + extension. If file already exists, append (number) after name.

      
       Example: createNonOverwriteFile(parent, "filename", "txt");
       // result of this method is "filename.txt"
       // if "filename.txt" already exists, the new file name is "filename(1).txt"
       
      Parameters:
      parent - The directory in which the file is to be created
      child - The child pathname string
      Returns:
      an instance of File
    • getExtension

      public static String getExtension(File file)
      Return extension of file. Extension excluded "." character.
      Parameters:
      file - file to find an extension
      Returns:
      extension of file
    • getExtension

      public static String getExtension(String filename)
      Return extension of file. Extension excluded "." character.
      Parameters:
      filename - file name
      Returns:
      extension of file
    • getName

      public static String getName(String filename)
      Return name of file without extension.
      Parameters:
      filename - file name
      Returns:
      name of file
    • recursiveFileURLList

      public static URL[] recursiveFileURLList(File basedDir)
      Recursive to get all resources in basedDir. Return array of file URL, or array length zero if no file found.
      Parameters:
      basedDir - root directory
      Returns:
      array of file URL
    • recursiveFileList

      public static File[] recursiveFileList(File basedDir)
      Recursive to get all files in basedDir. Return array of File, or array length zero if no file found.
      Parameters:
      basedDir - root directory
      Returns:
      array of File
    • createTempFile

      public static File createTempFile(String prefix, String suffix, String parent, boolean useUnique) throws IOException

      Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name. If this method returns successfully then it is guaranteed that:
      1. The file denoted by the returned abstract pathname did not exist before this method was invoked, and
      2. Neither this method nor any of its variants will return the same abstract pathname again in the current invocation of the virtual machine.
      This method provides only part of a temporary-file facility. To arrange for a file created by this method to be deleted automatically, use the track(File,Object) or java.io.File#deleteOnExit() method.
      If the parent argument is null then the system-dependent default temporary-file directory will be used. The default temporary-file directory is specified by the system property java.io.tmpdir.
      The complete structure of empty file is: prefix + unique_number + suffix
      Parameters:
      prefix - The prefix string to be used in generating the file's name; must be at least three characters long
      suffix - The suffix string to be used in generating the file's name; may be null, in which case the suffix ".tmp" will be used
      parent - The directory in which the file is to be created, or null if the default temporary-file directory is to be used
      useUnique - if true always create unique file name.
      Returns:
      An abstract pathname denoting a newly-created empty file
      Throws:
      IllegalArgumentException - If the prefix argument contains fewer than three characters
      IOException - If a file could not be created
    • createMrTempFile

      public static File createMrTempFile(String prefix, String suffix) throws IOException
      Create temporary file in the getMrTempDir() directory.

      This equals to createTempFile(prefix, suffix, getMrTempDir(), true).

      Parameters:
      prefix - The prefix string to be used in generating the file's name; must be at least three characters long
      suffix - The suffix string to be used in generating the file's name; may be null, in which case the suffix ".tmp" will be used
      Returns:
      An abstract pathname denoting a newly-created empty file
      Throws:
      IOException - If a file could not be created
    • getTempDir

      public static String getTempDir()
      Return system temporary directory.The default temporary-file directory is specified by the system property java.io.tmpdir. On UNIX systems the default value of this property is typically " /tmp" or "/var/tmp"; on Microsoft Windows systems it is typically " C:\\WINNT\\TEMP". A different value may be given to this system property when the Java virtual machine is invoked
      Returns:
      default temporary-file directory
    • getMrTempDir

      public static String getMrTempDir()
      Get MagicReport temporary directory.

      The MagicReport directory is getTempDir()+"mr/". The directory will be automatically created if it doesn't exist.

      Returns:
      MagicReport temporary directory.
      See Also:
    • copy

      public static int copy(InputStream input, OutputStream output) throws IOException
      Copy bytes from an InputStream to an OutputStream.
      This method buffers the input internally, so there is no need to use a BufferedInputStream.
      Parameters:
      input - the InputStream to read from
      output - the OutputStream to write to
      Returns:
      the number of bytes copied
      Throws:
      NullPointerException - if the input or output is null
      IOException - if an I/O error occurs
    • copy

      public static int copy(Reader input, Writer output) throws IOException
      Copy chars from a Reader to a Writer.
      This method buffers the input internally, so there is no need to use a BufferedReader.
      Parameters:
      input - the Reader to read from
      output - the Writer to write to
      Returns:
      the number of characters copied
      Throws:
      NullPointerException - if the input or output is null
      IOException - if an I/O error occurs
    • delete

      public static boolean delete(File file)
      Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the file and directory inside this file will be deleted recursively.
      Parameters:
      file - the file or directory to be deleted
      Returns:
      true if and only if the file or directory is successfully deleted; false otherwise
      Throws:
      SecurityException - If a security manager exists and its SecurityManager.checkDelete(java.lang.String) method denies delete access to the file
    • createDirectory

      public static void createDirectory(File file) throws IOException
      Create a directory denoted by abstract pathname. This method automatically remove special characters in name before create a directory. Special characters are one of the following: \/?*:"<>|
      Parameters:
      file - directory to be created.
      Throws:
      IOException - if false to create directory.
    • normalize

      public static File normalize(File file)
      Convert the given file to normal form. If the string is already in normal form then it is simply returned.
      Parameters:
      file - file
      Returns:
      normalized name
    • normalize

      public static String normalize(String name)
      Convert the given name string to normal form. If the string is already in normal form then it is simply returned.
      Parameters:
      name - name of file
      Returns:
      normalized name
    • track

      public static void track(File file, Object marker)
      Track the specified file, using the provided marker, deleting the file when the marker instance is garbage collected.
      Parameters:
      file - The file to be tracked.
      marker - The marker object used to track the file.
    • track

      public static void track(String path, Object marker)
      Track the specified file, using the provided marker, deleting the file when the marker instance is garbage collected.
      Parameters:
      path - The full path to the file to be tracked.
      marker - The marker object used to track the file.
    • getTemplateFile

      public static File getTemplateFile(String templateFileName, String templateLocation)
      Get Template file from template file name/path.
      Parameters:
      templateFileName - template file name or path
      templateLocation - template location
      Returns:
      template file
    • getTemplateFile

      public static File getTemplateFile(String templateFileName, String templateLocation, String extension, Object marker)
      Get Template file from template file name/path.
      Parameters:
      templateFileName - template file name or path
      templateLocation - template location
      extension - extension for template file. in case it has no extension
      marker - The marker object used to track the file.
      Returns:
      template file
    • getTemplateFile

      public static File getTemplateFile(String templateFileName, List<String> templateLocations, String extension, Object marker)
      Get Template file from template file name/path.
      Parameters:
      templateFileName - template file name or path
      templateLocations - list of possible locations
      extension - extension for template file. in case it has no extension
      marker - The marker object used to track the file.
      Returns:
      template file