FileUtils (IcedTea-Web: NetX )
Keywords: net.sourceforge.jnlp.util.FileUtils class
Date: 2018-03-18

net.sourceforge.jnlp.util

## Class FileUtils

- java.lang.Object
- - net.sourceforge.jnlp.util.FileUtils

-     public final class FileUtils
      extends java.lang.Object

  This class contains a few file-related utility functions.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `FileUtils.OpenFileResult` Indicates whether a file was successfully opened.

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static java.util.List<java.lang.Character>` | `INVALID_NAME`
    `static java.util.List<java.lang.Character>` | `INVALID_PATH` list of characters not allowed in filenames

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `FileUtils()`

  - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static void` | `createParentDir(java.io.File f)` Tries to create the ancestor directories of file f.
    `static void` | `createParentDir(java.io.File f, java.lang.String eMsg)` Tries to create the ancestor directories of file f.
    `static void` | `createRestrictedDirectory(java.io.File directory)` Creates a new directory with minimum permissions.
    `static void` | `createRestrictedFile(java.io.File file, boolean writableByOwner)` Creates a new file with minimum permissions.
    `static void` | `deleteWithErrMesg(java.io.File f)` Tries to delete file f.
    `static void` | `deleteWithErrMesg(java.io.File f, java.lang.String eMsg)` Tries to delete file f.
    `static java.lang.String` | `displayablePath(java.lang.String path)` Returns a String that is suitable for using in GUI elements for displaying (long) paths to users.
    `static java.lang.String` | `displayablePath(java.lang.String path, int visibleChars)` Return a String that is suitable for using in GUI elements for displaying paths to users.
    `static java.lang.String` | `getContentOfReader(java.io.Reader r)`
    `static java.lang.String` | `getContentOfStream(java.io.InputStream is)` utility method which can read from any stream as one long String
    `static java.lang.String` | `getContentOfStream(java.io.InputStream is, java.lang.String encoding)` utility method which can read from any stream as one long String
    `static java.nio.channels.FileLock` | `getFileLock(java.lang.String path, boolean shared, boolean allowBlock)` This will return a lock to the file specified.
    `static byte[]` | `getFileMD5Sum(java.io.File file, java.lang.String algorithm)`
    `static java.lang.String` | `loadFileAsString(java.io.File f)`
    `static java.lang.String` | `loadFileAsString(java.io.File f, java.lang.String encoding)`
    `static void` | `recursiveDelete(java.io.File file, java.io.File base)` Recursively delete everything under a directory.
    `static java.lang.String` | `sanitizeFileName(java.lang.String filename)` Given an input, return a sanitized form of the input suitable for use as a file/directory name
    `static java.lang.String` | `sanitizeFileName(java.lang.String filename, char substitute)`
    `static java.lang.String` | `sanitizePath(java.lang.String path)` Clean up a string by removing characters that can't appear in a local file name.
    `static java.lang.String` | `sanitizePath(java.lang.String path, char substitute)`
    `static void` | `saveFile(java.lang.String content, java.io.File f)` Method to save String as file in UTF-8 encoding.
    `static void` | `saveFile(java.lang.String content, java.io.File f, java.lang.String encoding)` Method to save String as file in specified encoding/.
    `static void` | `showCouldNotOpenDialog(java.awt.Component frame, java.lang.String message)` Show a dialog informing the user that the file could not be opened
    `static void` | `showCouldNotOpenFileDialog(java.awt.Component frame, java.lang.String filePath, FileUtils.OpenFileResult reason)` Show an error dialog indicating the file could not be opened, with a particular reason
    `static void` | `showCouldNotOpenFilepathDialog(java.awt.Component frame, java.lang.String filePath)` Show a generic error dialog indicating the file could not be opened
    `static void` | `showReadOnlyDialog(java.awt.Component frame)` Show a dialog informing the user that the file is currently read-only
    `static DirectoryValidator.DirectoryCheckResults` | `testDirectoryPermissions(java.io.File file)` Ensure that the parent directory of the file exists and that we are able to create and access files within this directory
    `static FileUtils.OpenFileResult` | `testFilePermissions(java.io.File file)` Verify that a given file object points to a real, accessible plain file.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### INVALID_PATH

          public static final java.util.List<java.lang.Character> INVALID_PATH

      list of characters not allowed in filenames

    - #### INVALID_NAME

          public static final java.util.List<java.lang.Character> INVALID_NAME

  - ### Constructor Detail

    - #### FileUtils

          public FileUtils()

  - ### Method Detail

    - #### sanitizePath

          public static java.lang.String sanitizePath(java.lang.String path)

      Clean up a string by removing characters that can't appear in a local file name.
      Parameters:
        `path` - the path to sanitize
      Returns:
        a sanitized version of the input which is suitable for using as a file path

    - #### sanitizePath

          public static java.lang.String sanitizePath(java.lang.String path,
                                                      char substitute)

    - #### sanitizeFileName

          public static java.lang.String sanitizeFileName(java.lang.String filename)

      Given an input, return a sanitized form of the input suitable for use as a file/directory name
      Parameters:
        `filename` - the filename to sanitize.
      Returns:
        a sanitized version of the input

    - #### sanitizeFileName

          public static java.lang.String sanitizeFileName(java.lang.String filename,
                                                          char substitute)

    - #### createRestrictedDirectory

          public static void createRestrictedDirectory(java.io.File directory)
                                                throws java.io.IOException

      Creates a new directory with minimum permissions. The directory is not readable or writable by anyone other than the owner. The parent directories are not created; they must exist before this is called.
      Parameters:
        `directory` - directory to be created
      Throws:
        `java.io.IOException` - if IO fails

    - #### createRestrictedFile

          public static void createRestrictedFile(java.io.File file,
                                                  boolean writableByOwner)
                                           throws java.io.IOException

      Creates a new file with minimum permissions. The file is not readable or writable by anyone other than the owner. If writeableByOnwer is false, even the owner can not write to it.
      Parameters:
        `file` - path to file
        `writableByOwner` - true if can be writable by owner
      Throws:
        `java.io.IOException` - if IO fails

    - #### createParentDir

          public static void createParentDir(java.io.File f,
                                             java.lang.String eMsg)
                                      throws java.io.IOException

      Tries to create the ancestor directories of file f. Throws an IOException if it can't be created (but not if it was already there).
      Parameters:
        `f` - file to provide parent directory
        `eMsg` - - the message to use for the exception. null if the file name is to be used.
      Throws:
        `java.io.IOException` - if the directory can't be created and doesn't exist.

    - #### createParentDir

          public static void createParentDir(java.io.File f)
                                      throws java.io.IOException

      Tries to create the ancestor directories of file f. Throws an IOException if it can't be created (but not if it was already there).
      Parameters:
        `f` - file which parent will be created
      Throws:
        `java.io.IOException` - if the directory can't be created and doesn't exist.

    - #### deleteWithErrMesg

          public static void deleteWithErrMesg(java.io.File f,
                                               java.lang.String eMsg)

      Tries to delete file f. If the file exists but couldn't be deleted, print an error message to stderr with the file name, or eMsg if eMsg is not null.
      Parameters:
        `f` - the file to be deleted
        `eMsg` - the message to print on failure (or null to print the the file name).

    - #### deleteWithErrMesg

          public static void deleteWithErrMesg(java.io.File f)

      Tries to delete file f. If the file exists but couldn't be deleted, print an error message to stderr with the file name.
      Parameters:
        `f` - the file to be deleted

    - #### testDirectoryPermissions

          public static DirectoryValidator.DirectoryCheckResults testDirectoryPermissions(java.io.File file)

      Ensure that the parent directory of the file exists and that we are able to create and access files within this directory
      Parameters:
        `file` - the `File` representing a Java Policy file to test
      Returns:
        a `DirectoryValidator.DirectoryCheckResults` object representing the results of the test

    - #### testFilePermissions

          public static FileUtils.OpenFileResult testFilePermissions(java.io.File file)

      Verify that a given file object points to a real, accessible plain file.
      Parameters:
        `file` - the `File` to verify
      Returns:
        an `FileUtils.OpenFileResult` representing the accessibility level of the file

    - #### showReadOnlyDialog

          public static void showReadOnlyDialog(java.awt.Component frame)

      Show a dialog informing the user that the file is currently read-only
      Parameters:
        `frame` - a `JFrame` to act as parent to this dialog

    - #### showCouldNotOpenFilepathDialog

          public static void showCouldNotOpenFilepathDialog(java.awt.Component frame,
                                                            java.lang.String filePath)

      Show a generic error dialog indicating the file could not be opened
      Parameters:
        `frame` - a `JFrame` to act as parent to this dialog
        `filePath` - a `String` representing the path to the file we failed to open

    - #### showCouldNotOpenFileDialog

          public static void showCouldNotOpenFileDialog(java.awt.Component frame,
                                                        java.lang.String filePath,
                                                        FileUtils.OpenFileResult reason)

      Show an error dialog indicating the file could not be opened, with a particular reason
      Parameters:
        `frame` - a `JFrame` to act as parent to this dialog
        `filePath` - a `String` representing the path to the file we failed to open
        `reason` - a `FileUtils.OpenFileResult` specifying more precisely why we failed to open the file

    - #### showCouldNotOpenDialog

          public static void showCouldNotOpenDialog(java.awt.Component frame,
                                                    java.lang.String message)

      Show a dialog informing the user that the file could not be opened
      Parameters:
        `frame` - a `JFrame` to act as parent to this dialog
        `message` - a `String` giving the specific reason the file could not be opened

    - #### displayablePath

          public static java.lang.String displayablePath(java.lang.String path)

      Returns a String that is suitable for using in GUI elements for displaying (long) paths to users.
      Parameters:
        `path` - a path that should be shortened
      Returns:
        a shortened path suitable for displaying to the user

    - #### displayablePath

          public static java.lang.String displayablePath(java.lang.String path,
                                                         int visibleChars)

      Return a String that is suitable for using in GUI elements for displaying paths to users. If the path is longer than visibleChars, it is truncated in a display-friendly way
      Parameters:
        `path` - a path that should be shorted
        `visibleChars` - the maximum number of characters that path should fit into. Also the length of the returned string
      Returns:
        a shortened path that contains limited number of chars

    - #### recursiveDelete

          public static void recursiveDelete(java.io.File file,
                                             java.io.File base)
                                      throws java.io.IOException

      Recursively delete everything under a directory. Works on either files or directories
      Parameters:
        `file` - the file object representing what to delete. Can be either a file or a directory.
        `base` - the directory under which the file and its subdirectories must be located
      Throws:
        `java.io.IOException` - on an io exception or if trying to delete something outside the base

    - #### getFileLock

          public static java.nio.channels.FileLock getFileLock(java.lang.String path,
                                                               boolean shared,
                                                               boolean allowBlock)
                                                        throws java.io.FileNotFoundException

      This will return a lock to the file specified.
      Parameters:
        `path` - File path to file we want to lock.
        `shared` - Specify if the lock will be a shared lock.
        `allowBlock` - Specify if we should block when we can not get the lock. Getting a shared lock will always block.
      Returns:
        FileLock if we were successful in getting a lock, otherwise null.
      Throws:
        `java.io.FileNotFoundException` - If the file does not exist.

    - #### saveFile

          public static void saveFile(java.lang.String content,
                                      java.io.File f)
                               throws java.io.IOException

      Method to save String as file in UTF-8 encoding.
      Parameters:
        `content` - which will be saved as it is saved in this String
        `f` - file to be saved. No warnings provided
      Throws:
        `java.io.IOException` - if save fails

    - #### saveFile

          public static void saveFile(java.lang.String content,
                                      java.io.File f,
                                      java.lang.String encoding)
                               throws java.io.IOException

      Method to save String as file in specified encoding/.
      Parameters:
        `content` - which will be saved as it is saved in this String
        `f` - file to be saved. No warnings provided
        `encoding` - of output byte representation
      Throws:
        `java.io.IOException` - if save fails

    - #### getContentOfStream

          public static java.lang.String getContentOfStream(java.io.InputStream is,
                                                            java.lang.String encoding)
                                                     throws java.io.IOException

      utility method which can read from any stream as one long String
      Parameters:
        `is` - stream
        `encoding` - the encoding to use to convert the bytes from the stream
      Returns:
        stream as string
      Throws:
        `java.io.IOException` - if connection can't be established or resource does not exist

    - #### getContentOfReader

          public static java.lang.String getContentOfReader(java.io.Reader r)
                                                     throws java.io.IOException

      Throws:
        `java.io.IOException`

    - #### getContentOfStream

          public static java.lang.String getContentOfStream(java.io.InputStream is)
                                                     throws java.io.IOException

      utility method which can read from any stream as one long String
      Parameters:
        `is` - stream
      Returns:
        stream as string
      Throws:
        `java.io.IOException` - if connection can't be established or resource does not exist

    - #### loadFileAsString

          public static java.lang.String loadFileAsString(java.io.File f)
                                                   throws java.io.IOException

      Throws:
        `java.io.IOException`

    - #### loadFileAsString

          public static java.lang.String loadFileAsString(java.io.File f,
                                                          java.lang.String encoding)
                                                   throws java.io.IOException

      Throws:
        `java.io.IOException`

    - #### getFileMD5Sum

          public static byte[] getFileMD5Sum(java.io.File file,
                                             java.lang.String algorithm)
                                      throws java.security.NoSuchAlgorithmException,
                                             java.io.FileNotFoundException,
                                             java.io.IOException

      Throws:
        `java.security.NoSuchAlgorithmException`
        `java.io.FileNotFoundException`
        `java.io.IOException`

Submit a bug or feature