Class IO

java.lang.Object
se.datadosen.util.IO

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

  • Constructor Details

    • IO

      public IO()
  • Method Details

    • ensureUnique

      public static File ensureUnique(File dir, String suggestedName)
    • readTextFile

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

      public static boolean isPreserveLastModified()
    • setPreserveLastModified

      public static void setPreserveLastModified(boolean preserveLastModified)
    • readTextFile

      public static String readTextFile(File file) throws IOException, NoSuchFileException, PermissionDeniedException
      Throws:
      IOException
      NoSuchFileException
      PermissionDeniedException
    • readTextFile

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

      public static String readTextUrl(URL textUrl) throws IOException
      Throws:
      IOException
    • readTextUrl

      public static String readTextUrl(URL textUrl, String userAgent) throws IOException
      Parameters:
      textUrl -
      userAgent -
      Returns:
      Throws:
      IOException
      Since:
      28.2
    • readTextUrl

      public static String readTextUrl(URL textUrl, SSLSocketFactory socketFactory) throws IOException
      Parameters:
      textUrl -
      socketFactory -
      Returns:
      Throws:
      IOException
      Since:
      20
    • readTextUrl

      public static String readTextUrl(URL textUrl, SSLSocketFactory socketFactory, String userAgent) throws IOException
      Parameters:
      textUrl -
      socketFactory -
      userAgent -
      Returns:
      Throws:
      IOException
      Since:
      28.2
    • readBytes

      public static byte[] readBytes(File f) throws IOException, NoSuchFileException, PermissionDeniedException
      Throws:
      IOException
      NoSuchFileException
      PermissionDeniedException
    • writeBytes

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

      public static void writeTextFile(String content, File file) throws IOException
      Throws:
      IOException
    • writeTextFile

      public static void writeTextFile(String content, File file, String encoding) throws IOException
      Throws:
      IOException
    • writeChangedTextFile

      @Deprecated public static boolean writeChangedTextFile(String content, File file, String encoding) throws IOException
      Deprecated.
      Use TargetPage instead of improved parallelism
      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:
      IOException
      Since:
      v4.5
    • readPropertyFile

      public static Properties readPropertyFile(File file) throws 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:
      IOException
    • readMapFile

      public static Map readMapFile(File file) throws 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:
      IOException
    • readMap

      public static Map readMap(byte[] bytes) throws IOException
      Throws:
      IOException
      Since:
      8.0
      See Also:
    • writeMapFile

      public static void writeMapFile(Map map, File f) throws 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:
      IOException
    • copyFilesUI

      public static void copyFilesUI(JFrame parent, File[] files, File dest) throws OperationAbortedException
      Throws:
      OperationAbortedException
    • copyFile

      public static void copyFile(File src, File dest) throws 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:
      IOException
    • copyFile

      public static void copyFile(String name, File dest) throws 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:
      IOException
    • copyFile

      public static void copyFile(String name, File dest, boolean forceCopy) throws 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:
      IOException
    • copyFile

      public static void copyFile(File src, File dest, boolean forceCopy) throws 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:
      IOException
    • copyFile

      public static boolean copyFile(File src, File dest, boolean forceCopy, boolean createHardLink) throws 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:
      IOException
    • downloadFile

      public static File downloadFile(URL url, File dest) throws PermissionDeniedException, 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
      IOException - if file can't be read
      Since:
      11
    • downloadFile

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

      public static void downloadZipFile(URL url, File destFolder) throws IOException
      Throws:
      IOException
    • readAll

      public static String readAll(Reader in) throws IOException
      Since 7.2
      Parameters:
      in - Reader
      Returns:
      String
      Throws:
      IOException
    • isSubdirectoryOf

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

      public static void copyDirectoryContent(String srcPath, File dest, boolean forceCopy) throws 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:
      IOException
    • copyDirectoryContent

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

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

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

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

      public static String parentPath(String path)
      Parameters:
      path -
      Returns:
      parent path (stripping stuff after last /)
      Since:
      24
    • baseName

      public static String baseName(AlbumObject ao)
      Parameters:
      ao -
      Returns:
      Base name if AlbumObject is not a folder. Folders returns their full name
      Since:
      28
    • baseName

      public static String baseName(String fullName)
      Returns:
      filename without extension
    • thumbFile

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

      public static boolean isThumbFile(File file)
    • baseName

      public static String baseName(File file)
      Returns:
      filename without extension
    • extensionOf

      public static String extensionOf(String name)
      Returns:
      extension of file name
    • lastPathComponentOf

      public static String lastPathComponentOf(String path)
      Since:
      13.5
    • extensionOf

      public static String extensionOf(File file)
      Returns:
      extension of file name
    • relativePath

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

      public static String relativePath(String fileString, String relString)
      Since:
      7.0.4
    • relativePath

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

      public static String urlEncode(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 String webSafe(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(String fileName)
    • webSafe

      public static File webSafe(File f)
    • combinePaths

      public static String combinePaths(String part1, 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 String combinePaths(String part1, 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(Object o)
      Return number of bytes a serialized version of the passed object would occupy
      Parameters:
      o - Object, must implement serializable
      Returns:
      Throws:
      RuntimeException
    • deepLastModified

      public static long deepLastModified(File dir) throws 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:
      IOException
      Since:
      5.2
    • deepLastModifiedFile

      public static long deepLastModifiedFile(File dir) throws 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:
      IOException
      Since:
      8.4.1
    • close

      public static void close(Closeable cl)
    • deleteDir

      public static void deleteDir(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(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(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(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 File resolvePath(File f, String path)
    • zip

      public static File zip(File f) throws ZipException, 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:
      ZipException
      IOException
      Since:
      16
    • zip

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

      public static File zip(File f, FileFilter filter) throws ZipException, IOException, OperationAbortedException
      Zip a file or folder to destination file. Put resulting zip file next to file
      Parameters:
      f - File or directory to zip
      filter - Allows monitoring each each zipped file or rejection
      Returns:
      zipped file
      Throws:
      ZipException
      IOException
      OperationAbortedException
      Since:
      16
    • zip

      public static void zip(File f, File zipFile, FileFilter filter) throws ZipException, IOException, OperationAbortedException
      Zip a file or folder to destination file
      Parameters:
      f - File or directory to zip
      zipFile - File Target for zipping
      filter - Allows monitoring each each zipped file or rejection. Throw OperationAbortedException to abort
      Throws:
      ZipException
      IOException
      OperationAbortedException
      Since:
      16
    • unzip

      public static int unzip(File zipFile, File destDir) throws ZipException, IOException
      Throws:
      ZipException
      IOException
    • unzip

      public static int unzip(ZipInputStream zis, File destDir) throws ZipException, IOException
      Throws:
      ZipException
      IOException
    • unzip

      public static int unzip(ZipInputStream zis, File destDir, boolean downloading) throws ZipException, IOException
      Throws:
      ZipException
      IOException