Class IO


  • public class IO
    extends java.lang.Object
    Utility class to simplify common file related operations This class is LGPL licensed. Just keep this notice intact
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String reserved  
      static java.lang.String rfc3986Reserved  
    • Constructor Summary

      Constructors 
      Constructor Description
      IO()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String baseName​(java.io.File file)  
      static java.lang.String baseName​(java.lang.String fullName)  
      static void close​(java.io.Closeable cl)  
      static java.lang.String combinePaths​(java.lang.String part1, java.lang.String part2)
      Combine two path parts so that there is one and only one path separator between nomatter how each part looks.
      static java.lang.String combinePaths​(java.lang.String part1, java.lang.String part2, char separator)
      Combine two path parts so that there is one and only one path separator between no matter how each part looks.
      static void copyDirectoryContent​(java.io.File srcDir, java.io.File dest, boolean forceCopy)  
      static void copyDirectoryContent​(java.io.File srcDir, java.io.File dest, boolean forceCopy, java.io.FileFilter filter)  
      static void copyDirectoryContent​(java.io.File srcDir, java.io.File dest, boolean forceCopy, java.io.FileFilter filter, boolean createHardLink)  
      static void copyDirectoryContent​(java.lang.String srcPath, java.io.File dest, boolean forceCopy)
      Copy a directory (including its subdirectories) to destination directory.
      static void copyFile​(java.io.File src, java.io.File dest)
      Copy file or directory to destination.
      static void copyFile​(java.io.File src, java.io.File dest, boolean forceCopy)
      Copy file or directory to destination.
      static boolean copyFile​(java.io.File src, java.io.File dest, boolean forceCopy, boolean createHardLink)
      Copy file or directory to destination.
      static void copyFile​(java.lang.String name, java.io.File dest)
      Copy file or directory to destination.
      static void copyFile​(java.lang.String name, java.io.File dest, boolean forceCopy)
      Copy file or directory to destination.
      static void copyFilesUI​(javax.swing.JFrame parent, java.io.File[] files, java.io.File dest)  
      static void createDirectories​(java.nio.file.Path path, java.nio.file.attribute.FileAttribute<?>... attrs)
      Like Files.createDirectories, but won't throw FileAlreadyExistsException if path already exists and is a symlink to a directory
      static long deepLastModified​(java.io.File dir)
      Figure out the latest "last modified" file date of any file or directory under the directory passed as argument including files in subdirectories
      static long deepLastModifiedFile​(java.io.File dir)
      Figure out the latest "last modified" file date of any file (not directory) under the directory passed as argument including files in subdirectories
      static void deleteDir​(java.io.File dir)
      The very powerful (and dangerous) delete directory Performs true deletes since jAlbum 8.7.2.
      static java.io.File downloadFile​(java.net.URL url, java.io.File dest)
      Download file to destination folder or file
      static void downloadFile​(java.net.URL url, java.io.OutputStream out)
      Download file to output stream Uses a timeout of 10 seconds
      static void downloadZipFile​(java.net.URL url, java.io.File destFolder)  
      static void emptyDir​(java.io.File dir)
      Delete directory contents recursively, but leave directory Does not follow symbolic links when running on Java 7+ (Since jAlbum 12.1)
      static java.io.File ensureUnique​(java.io.File dir, java.lang.String suggestedName)  
      static java.lang.String extensionOf​(java.io.File file)  
      static java.lang.String extensionOf​(java.lang.String name)  
      static boolean isPreserveLastModified()  
      static boolean isSubdirectoryOf​(java.io.File subdir, java.io.File dir)
      Test if subdir is a subdirectory of dir (or one of its subdirectories recursively)
      static boolean isThumbFile​(java.io.File file)  
      static boolean isWebSafe​(java.lang.String fileName)  
      static java.lang.String lastPathComponentOf​(java.lang.String path)  
      static java.lang.String readAll​(java.io.Reader in)
      Since 7.2
      static byte[] readBytes​(java.io.File f)  
      static java.util.Map readMap​(byte[] bytes)  
      static java.util.Map readMapFile​(java.io.File file)
      Read an equal sign separated list of name=value pairs into a Map object.
      static java.util.Properties readPropertyFile​(java.io.File file)
      Read an equal sign separated list of name=value pairs into a Property object.
      static java.lang.String readTextFile​(java.io.File file)  
      static java.lang.String readTextFile​(java.io.File file, java.lang.String encoding)
      Read a text file into a String.
      static java.lang.String readTextFile​(java.lang.String fileName)
      Since v15.5 reimplemented to throw NoSuchFileException and PermissionDeniedException
      static java.lang.String readTextUrl​(java.net.URL textUrl)  
      static boolean recycle​(java.io.File dir)
      Will attempt to move the directory to the recycle bin If recycling fails, will attempt to delete permanently On Mac.
      static boolean recycle​(java.io.File dir, boolean recycleOnly)
      Will attempt to move the directory to the recycle bin On Mac.
      static java.lang.String relativePath​(java.io.File file, java.io.File rel)  
      static java.lang.String relativePath​(java.lang.String fileString, java.lang.String relString)  
      static java.lang.String relativePath​(java.lang.String fileString, java.lang.String relString, char separator)  
      static java.io.File resolvePath​(java.io.File f, java.lang.String path)  
      static void setPreserveLastModified​(boolean preserveLastModified)  
      static int sizeof​(java.lang.Object o)
      Return number of bytes a serialized version of the passed object would occupy
      static java.io.File thumbFile​(java.io.File file)
      Get the corresponding thumbnail file for given file
      static int unzip​(java.io.File zipFile, java.io.File destDir)  
      static int unzip​(java.util.zip.ZipInputStream zis, java.io.File destDir)  
      static int unzip​(java.util.zip.ZipInputStream zis, java.io.File destDir, boolean downloading)  
      static java.lang.String urlEncode​(java.lang.String s)
      URLencoding based on rfc3986 but with additions to reserved words to comply with Mac OSX restrictions; Reference: http://en.wikipedia.org/wiki/Percent-encoding Prior to jAlbum 8.1.5, this encoder attempted ISO8859-1 encoding of characters > 127 Only if a character wasn't encodable using ISO8859-1 it reverted to UTF-8 encoding.
      static java.io.File webSafe​(java.io.File f)  
      static java.lang.String webSafe​(java.lang.String fileName)  
      static void writeBytes​(byte[] buf, java.io.File f)  
      static boolean writeChangedTextFile​(java.lang.String content, java.io.File file, java.lang.String encoding)
      Write text to text file if this means a change to the file content
      static void writeMapFile​(java.util.Map map, java.io.File f)
      Write a Map to file.
      static void writeTextFile​(java.lang.String content, java.io.File file)  
      static void writeTextFile​(java.lang.String content, java.io.File file, java.lang.String encoding)  
      static java.io.File zip​(java.io.File f)
      Zip a file or folder to destination file.
      static void zip​(java.io.File f, java.io.File zipFile)
      Zip a file or folder to destination file
      static void zip​(java.io.File f, java.io.File zipFile, java.util.function.Consumer<java.io.File> monitor)
      Zip a file or folder to destination file
      static java.io.File zip​(java.io.File f, java.util.function.Consumer<java.io.File> monitor)
      Zip a file or folder to destination file.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • IO

        public IO()
    • Method Detail

      • ensureUnique

        public static java.io.File ensureUnique​(java.io.File dir,
                                                java.lang.String suggestedName)
      • readTextFile

        public static java.lang.String readTextFile​(java.io.File file,
                                                    java.lang.String encoding)
                                             throws java.io.IOException,
                                                    java.nio.file.NoSuchFileException,
                                                    PermissionDeniedException
        Read a text file into a String. Autodetects UTF-8 and UTF-16 encodings
        Parameters:
        file -
        encoding -
        Returns:
        Throws:
        java.io.IOException
        java.nio.file.NoSuchFileException
        PermissionDeniedException
      • isPreserveLastModified

        public static boolean isPreserveLastModified()
      • setPreserveLastModified

        public static void setPreserveLastModified​(boolean preserveLastModified)
      • readTextFile

        public static java.lang.String readTextFile​(java.io.File file)
                                             throws java.io.IOException,
                                                    java.nio.file.NoSuchFileException,
                                                    PermissionDeniedException
        Throws:
        java.io.IOException
        java.nio.file.NoSuchFileException
        PermissionDeniedException
      • readTextFile

        public static java.lang.String readTextFile​(java.lang.String fileName)
                                             throws java.io.IOException,
                                                    java.nio.file.NoSuchFileException,
                                                    PermissionDeniedException
        Since v15.5 reimplemented to throw NoSuchFileException and PermissionDeniedException
        Parameters:
        fileName -
        Returns:
        Throws:
        java.io.IOException
        java.nio.file.NoSuchFileException
        PermissionDeniedException
      • readTextUrl

        public static java.lang.String readTextUrl​(java.net.URL textUrl)
                                            throws java.io.IOException
        Throws:
        java.io.IOException
      • writeBytes

        public static void writeBytes​(byte[] buf,
                                      java.io.File f)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • writeTextFile

        public static void writeTextFile​(java.lang.String content,
                                         java.io.File file)
                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • writeTextFile

        public static void writeTextFile​(java.lang.String content,
                                         java.io.File file,
                                         java.lang.String encoding)
                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • writeChangedTextFile

        public static boolean writeChangedTextFile​(java.lang.String content,
                                                   java.io.File file,
                                                   java.lang.String encoding)
                                            throws java.io.IOException
        Write text to text file if this means a change to the file content
        Parameters:
        content - Text to write
        file - File to be written. Will make a comparison before writing if file already exists
        encoding - Text encoding to use
        Returns:
        boolean true if a change was actually made
        Throws:
        java.io.IOException
        Since:
        v4.5
      • readPropertyFile

        public static java.util.Properties readPropertyFile​(java.io.File file)
                                                     throws java.io.IOException
        Read an equal sign separated list of name=value pairs into a Property object. Does not handle names with spaces and is limited to ISO-8859-1. See readMapFile()
        Parameters:
        file -
        Returns:
        Throws:
        java.io.IOException
      • readMapFile

        public static java.util.Map readMapFile​(java.io.File file)
                                         throws java.io.IOException
        Read an equal sign separated list of name=value pairs into a Map object. Handles names containing spaces. Respects current system encoding and auto-detects UTF-8 and UTF-16 encoding. Lines beginning with # are considered comment lines Values ending in backslash is considered to be a multi-line value Backslashes in values are expressed as double backslashes
        Parameters:
        file -
        Returns:
        Throws:
        java.io.IOException
      • readMap

        public static java.util.Map readMap​(byte[] bytes)
                                     throws java.io.IOException
        Throws:
        java.io.IOException
        Since:
        8.0
        See Also:
        readMapFile(java.io.File)
      • writeMapFile

        public static void writeMapFile​(java.util.Map map,
                                        java.io.File f)
                                 throws java.io.IOException
        Write a Map to file. Tries to write using system encoding. If that doesn't work, uses UTF-8. Multi-line values result in lines ending in backslashes Backslashes in values generate double backslashes
        Parameters:
        map -
        f -
        Throws:
        java.io.IOException
      • copyFile

        public static void copyFile​(java.io.File src,
                                    java.io.File dest)
                             throws java.io.IOException
        Copy file or directory to destination. Preserves file modification date
        Parameters:
        src - to copy
        dest - Destination. May point either to a file or a directory
        Throws:
        java.io.IOException
      • copyFile

        public static void copyFile​(java.lang.String name,
                                    java.io.File dest)
                             throws java.io.IOException
        Copy file or directory to destination. Preserves file modification date
        Parameters:
        name - Name of file to copy
        dest - Destination. May point either to a file or a directory
        Throws:
        java.io.IOException
      • copyFile

        public static void copyFile​(java.lang.String name,
                                    java.io.File dest,
                                    boolean forceCopy)
                             throws java.io.IOException
        Copy file or directory to destination. Preserves file modification date
        Parameters:
        name - Name of file to copy
        dest - Destination. May point either to a file or a directory
        forceCopy - Always copy, even if destination file already exist
        Throws:
        java.io.IOException
      • copyFile

        public static void copyFile​(java.io.File src,
                                    java.io.File dest,
                                    boolean forceCopy)
                             throws java.io.IOException
        Copy file or directory to destination. Preserves file modification date
        Parameters:
        src - File to copy
        dest - Destination. May point either to a file or a directory
        forceCopy - Always copy, even if destination file already exist
        Throws:
        java.io.IOException
      • copyFile

        public static boolean copyFile​(java.io.File src,
                                       java.io.File dest,
                                       boolean forceCopy,
                                       boolean createHardLink)
                                throws java.io.IOException
        Copy file or directory to destination. Preserves file modification date
        Parameters:
        src - File to copy
        dest - Destination. May point either to a file or a directory
        forceCopy - Always copy, even if destination file already exist
        createHardLink - Attempt to make a hard link instead of copy
        Returns:
        true if directed to make hard link, unless linking fails
        Throws:
        java.io.IOException
      • downloadFile

        public static java.io.File downloadFile​(java.net.URL url,
                                                java.io.File dest)
                                         throws PermissionDeniedException,
                                                java.io.IOException
        Download file to destination folder or file
        Parameters:
        url - Where to get file
        dest - File or folder to write to
        Returns:
        Destination file
        Throws:
        PermissionDeniedException - if file can't be created
        java.io.IOException - if file can't be read
        Since:
        11
      • downloadFile

        public static void downloadFile​(java.net.URL url,
                                        java.io.OutputStream out)
                                 throws java.io.IOException
        Download file to output stream Uses a timeout of 10 seconds
        Parameters:
        url -
        out -
        Throws:
        java.io.IOException
        Since:
        11
      • downloadZipFile

        public static void downloadZipFile​(java.net.URL url,
                                           java.io.File destFolder)
                                    throws java.io.IOException
        Throws:
        java.io.IOException
      • readAll

        public static java.lang.String readAll​(java.io.Reader in)
                                        throws java.io.IOException
        Since 7.2
        Parameters:
        in - Reader
        Returns:
        String
        Throws:
        java.io.IOException
      • isSubdirectoryOf

        public static boolean isSubdirectoryOf​(java.io.File subdir,
                                               java.io.File dir)
        Test if subdir is a subdirectory of dir (or one of its subdirectories recursively)
      • copyDirectoryContent

        public static void copyDirectoryContent​(java.lang.String srcPath,
                                                java.io.File dest,
                                                boolean forceCopy)
                                         throws java.io.IOException
        Copy a directory (including its subdirectories) to destination directory.
        Parameters:
        srcPath - Path to source directory
        dest - Destination directory. Create one if not existing already
        forceCopy - Always copy, even if destination files already exists
        Throws:
        java.io.IOException
      • copyDirectoryContent

        public static void copyDirectoryContent​(java.io.File srcDir,
                                                java.io.File dest,
                                                boolean forceCopy)
                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • copyDirectoryContent

        public static void copyDirectoryContent​(java.io.File srcDir,
                                                java.io.File dest,
                                                boolean forceCopy,
                                                java.io.FileFilter filter)
                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • copyDirectoryContent

        public static void copyDirectoryContent​(java.io.File srcDir,
                                                java.io.File dest,
                                                boolean forceCopy,
                                                java.io.FileFilter filter,
                                                boolean createHardLink)
                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • createDirectories

        public static void createDirectories​(java.nio.file.Path path,
                                             java.nio.file.attribute.FileAttribute<?>... attrs)
                                      throws java.io.IOException
        Like Files.createDirectories, but won't throw FileAlreadyExistsException if path already exists and is a symlink to a directory
        Parameters:
        path -
        attrs -
        Throws:
        java.io.IOException
        Since:
        13.7
      • baseName

        public static java.lang.String baseName​(java.lang.String fullName)
        Returns:
        filename without extension
      • thumbFile

        public static java.io.File thumbFile​(java.io.File file)
        Get the corresponding thumbnail file for given file
        Since:
        8.0.9.1
      • isThumbFile

        public static boolean isThumbFile​(java.io.File file)
      • baseName

        public static java.lang.String baseName​(java.io.File file)
        Returns:
        filename without extension
      • extensionOf

        public static java.lang.String extensionOf​(java.lang.String name)
        Returns:
        extension of file name
      • lastPathComponentOf

        public static java.lang.String lastPathComponentOf​(java.lang.String path)
        Since:
        13.5
      • extensionOf

        public static java.lang.String extensionOf​(java.io.File file)
        Returns:
        extension of file name
      • relativePath

        public static java.lang.String relativePath​(java.io.File file,
                                                    java.io.File rel)
        Parameters:
        file -
        rel -
        Returns:
        a path to file that is relative to rel
      • relativePath

        public static java.lang.String relativePath​(java.lang.String fileString,
                                                    java.lang.String relString)
        Since:
        7.0.4
      • relativePath

        public static java.lang.String relativePath​(java.lang.String fileString,
                                                    java.lang.String relString,
                                                    char separator)
        Since:
        7.1
      • urlEncode

        public static java.lang.String urlEncode​(java.lang.String s)
        URLencoding based on rfc3986 but with additions to reserved words to comply with Mac OSX restrictions; Reference: http://en.wikipedia.org/wiki/Percent-encoding Prior to jAlbum 8.1.5, this encoder attempted ISO8859-1 encoding of characters > 127 Only if a character wasn't encodable using ISO8859-1 it reverted to UTF-8 encoding. Now UTF-8 is used for all characters > 127
      • webSafe

        public static java.lang.String webSafe​(java.lang.String fileName)
        Parameters:
        fileName -
        Returns:
        Web safe version of file name that doesn't contain any illegal characters (replaced with -)
        Since:
        11.3
      • isWebSafe

        public static boolean isWebSafe​(java.lang.String fileName)
      • webSafe

        public static java.io.File webSafe​(java.io.File f)
      • combinePaths

        public static java.lang.String combinePaths​(java.lang.String part1,
                                                    java.lang.String part2)
        Combine two path parts so that there is one and only one path separator between nomatter how each part looks.
        Parameters:
        part1 -
        part2 -
        Returns:
        String combined path (using / as separator
      • combinePaths

        public static java.lang.String combinePaths​(java.lang.String part1,
                                                    java.lang.String part2,
                                                    char separator)
        Combine two path parts so that there is one and only one path separator between no matter how each part looks.
        Parameters:
        part1 -
        part2 -
        separator -
        Returns:
        String combined path
      • sizeof

        public static int sizeof​(java.lang.Object o)
        Return number of bytes a serialized version of the passed object would occupy
        Parameters:
        o - Object, must implement serializable
        Returns:
        Throws:
        java.lang.RuntimeException
      • deepLastModified

        public static long deepLastModified​(java.io.File dir)
                                     throws java.io.IOException
        Figure out the latest "last modified" file date of any file or directory under the directory passed as argument including files in subdirectories
        Parameters:
        dir -
        Returns:
        Throws:
        java.io.IOException
        Since:
        5.2
      • deepLastModifiedFile

        public static long deepLastModifiedFile​(java.io.File dir)
                                         throws java.io.IOException
        Figure out the latest "last modified" file date of any file (not directory) under the directory passed as argument including files in subdirectories
        Throws:
        java.io.IOException
        Since:
        8.4.1
      • close

        public static void close​(java.io.Closeable cl)
      • deleteDir

        public static void deleteDir​(java.io.File dir)
        The very powerful (and dangerous) delete directory Performs true deletes since jAlbum 8.7.2. Previously attempted to recycle first Does not follow symbolic links when running on Java 7+ (Since jAlbum 12.1)
        Parameters:
        dir - Directory to delete
        Since:
        8.5
      • emptyDir

        public static void emptyDir​(java.io.File dir)
        Delete directory contents recursively, but leave directory Does not follow symbolic links when running on Java 7+ (Since jAlbum 12.1)
        Parameters:
        dir -
        Since:
        v12
      • recycle

        public static boolean recycle​(java.io.File dir)
        Will attempt to move the directory to the recycle bin If recycling fails, will attempt to delete permanently On Mac. Java version 1.6.0_17 and similar on Java 1.5 is required
        Parameters:
        dir - Directory to recycle
        dir -
        Returns:
        true if directory was recycled instead of deleted
        Since:
        8.7.2
      • recycle

        public static boolean recycle​(java.io.File dir,
                                      boolean recycleOnly)
        Will attempt to move the directory to the recycle bin On Mac. Java version 1.6.0_17 and similar on Java 1.5 is required
        Parameters:
        dir - Directory to recycle
        recycleOnly - If false, will attempt to delete permanently if recycling fails
        Returns:
        true if directory was recycled instead of deleted
        Since:
        8.7.2
      • resolvePath

        public static java.io.File resolvePath​(java.io.File f,
                                               java.lang.String path)
      • zip

        public static java.io.File zip​(java.io.File f)
                                throws java.util.zip.ZipException,
                                       java.io.IOException
        Zip a file or folder to destination file. Put resulting zip file next to file
        Parameters:
        f - File or directory to zip
        Returns:
        zipped file
        Throws:
        java.util.zip.ZipException
        java.io.IOException
        Since:
        16
      • zip

        public static void zip​(java.io.File f,
                               java.io.File zipFile)
                        throws java.util.zip.ZipException,
                               java.io.IOException
        Zip a file or folder to destination file
        Parameters:
        f - File or directory to zip
        zipFile - File Target for zipping
        Throws:
        java.util.zip.ZipException
        java.io.IOException
        Since:
        16
      • zip

        public static java.io.File zip​(java.io.File f,
                                       java.util.function.Consumer<java.io.File> monitor)
                                throws java.util.zip.ZipException,
                                       java.io.IOException,
                                       OperationAbortedException
        Zip a file or folder to destination file. Put resulting zip file next to file
        Parameters:
        f - File or directory to zip
        monitor - Monitor each zipped file. Throw OperationAbortedException to abort
        Returns:
        zipped file
        Throws:
        java.util.zip.ZipException
        java.io.IOException
        OperationAbortedException
        Since:
        16
      • zip

        public static void zip​(java.io.File f,
                               java.io.File zipFile,
                               java.util.function.Consumer<java.io.File> monitor)
                        throws java.util.zip.ZipException,
                               java.io.IOException,
                               OperationAbortedException
        Zip a file or folder to destination file
        Parameters:
        f - File or directory to zip
        zipFile - File Target for zipping
        monitor - Monitor each zipped file. Throw OperationAbortedException to abort
        Throws:
        java.util.zip.ZipException
        java.io.IOException
        OperationAbortedException
        Since:
        16
      • unzip

        public static int unzip​(java.io.File zipFile,
                                java.io.File destDir)
                         throws java.util.zip.ZipException,
                                java.io.IOException
        Throws:
        java.util.zip.ZipException
        java.io.IOException
      • unzip

        public static int unzip​(java.util.zip.ZipInputStream zis,
                                java.io.File destDir)
                         throws java.util.zip.ZipException,
                                java.io.IOException
        Throws:
        java.util.zip.ZipException
        java.io.IOException
      • unzip

        public static int unzip​(java.util.zip.ZipInputStream zis,
                                java.io.File destDir,
                                boolean downloading)
                         throws java.util.zip.ZipException,
                                java.io.IOException
        Throws:
        java.util.zip.ZipException
        java.io.IOException