Class IO
java.lang.Object
se.datadosen.util.IO
Utility class to simplify common file related operations This class is LGPL
licensed. Just keep this notice intact
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final Filestatic final Stringstatic final Stringstatic final ExecutorService -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Stringstatic Stringstatic StringbaseName(AlbumObject ao) static voidstatic StringcombinePaths(String part1, String part2) Combine two path parts so that there is one and only one path separator between nomatter how each part looks.static StringcombinePaths(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.static voidcopyDirectoryContent(File srcDir, File dest, boolean forceCopy) static voidcopyDirectoryContent(File srcDir, File dest, boolean forceCopy, FileFilter filter) static voidcopyDirectoryContent(File srcDir, File dest, boolean forceCopy, FileFilter filter, boolean createHardLink) static voidcopyDirectoryContent(String srcPath, File dest, boolean forceCopy) Copy a directory (including its subdirectories) to destination directory.static voidCopy file or directory to destination.static voidCopy file or directory to destination.static booleanCopy file or directory to destination.static voidCopy file or directory to destination.static voidCopy file or directory to destination.static voidcreateDirectories(Path path, FileAttribute<?>... attrs) Like Files.createDirectories, but won't throw FileAlreadyExistsException if path already exists and is a symlink to a directorystatic longdeepLastModified(File dir) Figure out the latest "last modified" file date of any file or directory under the directory passed as argument including files in subdirectoriesstatic longdeepLastModifiedFile(File dir) Figure out the latest "last modified" file date of any file (not directory) under the directory passed as argument including files in subdirectoriesstatic voidThe very powerful (and dangerous) delete directory Performs true deletes since jAlbum 8.7.2.static voiddeleteIfEmpty(File dir) Delete directory if it is empty (Ignore OS specific system files)static FiledownloadFile(URL url, File dest) Download file to destination folder or filestatic voiddownloadFile(URL url, OutputStream out) Download file to output stream Uses a timeout of 10 secondsstatic voiddownloadZipFile(URL url, File destFolder) static voidDelete directory contents recursively, but leave directory Does not follow symbolic links when running on Java 7+ (Since jAlbum 12.1)static FileensureUnique(File dir, String suggestedName) static StringextensionOf(File file) static StringextensionOf(String name) static booleanCheck whether specified directory is empty.static booleanstatic booleanisSubdirectoryOf(File subdir, File dir) Test if subdir is a subdirectory of dir (or one of its subdirectories recursively)static booleanisSystemFile(String fileName) static booleanisThumbFile(File file) static booleanstatic StringlastPathComponentOf(String path) static booleanMove file to destination Operation ignored if source file is missing Target is overwritten if already existsstatic StringparentPath(String path) static StringSince 7.2static byte[]static MapreadMap(byte[] bytes) readMap(byte[] bytes, boolean maintainOrdering) static MapreadMapFile(File file) Read an equal sign separated list of name=value pairs into a Map object.readMapFile(File file, boolean maintainOrdering) Read an equal sign separated list of name=value pairs into a Map object.static PropertiesreadPropertyFile(File file) Read an equal sign separated list of name=value pairs into a Property object.static StringreadTextFile(File file) static StringreadTextFile(File file, String encoding) Read a text file into a String.static StringreadTextFile(String fileName) Since v15.5 reimplemented to throw NoSuchFileException and PermissionDeniedExceptionstatic StringreadTextUrl(URL textUrl) static StringreadTextUrl(URL textUrl, String userAgent) static StringreadTextUrl(URL textUrl, SSLSocketFactory socketFactory) static StringreadTextUrl(URL textUrl, SSLSocketFactory socketFactory, String userAgent) static booleanWill attempt to move the directory to the recycle bin If recycling fails, will attempt to delete permanently On Mac.static booleanWill attempt to move the directory to the recycle bin On Mac.static StringrelativePath(File file, File rel) static StringrelativePath(String fileString, String relString) static StringrelativePath(String fileString, String relString, char separator) static Stringstatic FileresolvePath(File f, String path) static voidsetPreserveLastModified(boolean preserveLastModified) static intReturn number of bytes a serialized version of the passed object would occupystatic FileGet the corresponding thumbnail file for given filestatic intstatic intunzip(ZipInputStream zis, File destDir) static intunzip(ZipInputStream zis, File destDir, boolean downloading) static StringURLencoding 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 Stringstatic Stringstatic Filestatic Stringstatic voidwriteBytes(byte[] buf, File f) static booleanwriteChangedTextFile(String content, File file, String encoding) Deprecated.Use TargetPage instead for improved parallelismstatic voidwriteMapFile(Map map, File f) Write a Map to file.static voidwriteTextFile(String content, File file) static voidwriteTextFile(String content, File file, String encoding) static FileZip a file or folder to destination file.static voidZip a file or folder to destination filestatic Filezip(File f, FileFilter filter) Zip a file or folder to destination file.static voidzip(File f, File zipFile, FileFilter filter) Zip a file or folder to destination file
-
Field Details
-
IMPORT_DIR
-
CHROME_USER_AGENT
- Since:
- 28.2
- See Also:
-
workerPool
-
rfc3986Reserved
- See Also:
-
reserved
- See Also:
-
-
Constructor Details
-
IO
public IO()
-
-
Method Details
-
ensureUnique
-
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:
IOExceptionNoSuchFileExceptionPermissionDeniedException
-
isPreserveLastModified
public static boolean isPreserveLastModified() -
setPreserveLastModified
public static void setPreserveLastModified(boolean preserveLastModified) -
readTextFile
public static String readTextFile(File file) 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:
IOExceptionNoSuchFileExceptionPermissionDeniedException
-
readTextUrl
- Throws:
IOException
-
readTextUrl
- Parameters:
textUrl-userAgent-- Returns:
- Throws:
IOException- Since:
- 28.2
-
readTextUrl
- 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 -
writeBytes
- Throws:
IOException
-
writeTextFile
- Throws:
IOException
-
writeTextFile
- Throws:
IOException
-
writeChangedTextFile
@Deprecated public static boolean writeChangedTextFile(String content, File file, String encoding) throws IOException Deprecated.Use TargetPage instead for improved parallelismWrite text to text file if this means a change to the file content- Parameters:
content- Text to writefile- File to be written. Will make a comparison before writing if file already existsencoding- Text encoding to use- Returns:
- boolean true if a change was actually made
- Throws:
IOException- Since:
- v4.5
-
readPropertyFile
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
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
-
readMapFile
public static Map<String,Object> readMapFile(File file, boolean maintainOrdering) 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-maintainOrdering-- Returns:
- Throws:
IOException- Since:
- 31.2
-
readMap
- Throws:
IOException- Since:
- 8.0
- See Also:
-
readMap
- Parameters:
bytes-maintainOrdering-- Returns:
- Throws:
IOException- Since:
- 31.2
-
writeMapFile
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
-
move
Move file to destination Operation ignored if source file is missing Target is overwritten if already exists- Parameters:
src- Source file. Operation ignored if source doesn't existdest- Destination file or directory- Returns:
- true if move was performed ok
- Since:
- 37.6
-
copyFile
Copy file or directory to destination. Preserves file modification date- Parameters:
src- to copydest- Destination. May point either to a file or a directory- Throws:
IOException
-
copyFile
Copy file or directory to destination. Preserves file modification date- Parameters:
name- Name of file to copydest- Destination. May point either to a file or a directory- Throws:
IOException
-
copyFile
Copy file or directory to destination. Preserves file modification date- Parameters:
name- Name of file to copydest- Destination. May point either to a file or a directoryforceCopy- Always copy, even if destination file already exist- Throws:
IOException
-
copyFile
Copy file or directory to destination. Preserves file modification date- Parameters:
src- File to copydest- Destination. May point either to a file or a directoryforceCopy- 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 copydest- Destination. May point either to a file or a directoryforceCopy- Always copy, even if destination file already existcreateHardLink- Attempt to make a hard link instead of copy- Returns:
- true if directed to make hard link, unless linking fails
- Throws:
IOException
-
downloadFile
Download file to destination folder or file- Parameters:
url- Where to get filedest- File or folder to write to- Returns:
- Destination file
- Throws:
PermissionDeniedException- if file can't be createdIOException- if file can't be read- Since:
- 11
-
downloadFile
Download file to output stream Uses a timeout of 10 seconds- Parameters:
url-out-- Throws:
IOException- Since:
- 11
-
downloadZipFile
- Throws:
IOException
-
readAll
Since 7.2- Parameters:
in- Reader- Returns:
- String
- Throws:
IOException
-
isSubdirectoryOf
-
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 directorydest- Destination directory. Create one if not existing alreadyforceCopy- 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
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
-
baseName
- Parameters:
ao-- Returns:
- Base name if AlbumObject is not a folder. Folders returns their full name
- Since:
- 28
-
baseName
-
thumbFile
-
isThumbFile
-
baseName
-
extensionOf
-
lastPathComponentOf
-
extensionOf
-
relativePath
-
relativePath
-
relativePath
-
webPath
-
webPath
-
urlEncode
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 -
removeDiacritics
-
webSafe
-
isWebSafe
-
webSafe
-
combinePaths
-
combinePaths
-
sizeof
Return number of bytes a serialized version of the passed object would occupy- Parameters:
o- Object, must implement serializable- Returns:
- Throws:
RuntimeException
-
deepLastModified
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
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
-
isSystemFile
- Parameters:
fileName-- Returns:
- Since:
- 38
-
isEmpty
Check whether specified directory is empty. Ignores OS specific system files like ".DS_Store" on Mac- Parameters:
dir-- Returns:
- Since:
- 38
-
deleteIfEmpty
Delete directory if it is empty (Ignore OS specific system files)- Parameters:
dir-
-
deleteDir
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
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
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 recycledir-- Returns:
- true if directory was recycled instead of deleted
- Since:
- 8.7.2
-
recycle
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 recyclerecycleOnly- If false, will attempt to delete permanently if recycling fails- Returns:
- true if directory was recycled instead of deleted
- Since:
- 8.7.2
-
resolvePath
-
zip
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:
ZipExceptionIOException- Since:
- 16
-
zip
Zip a file or folder to destination file- Parameters:
f- File or directory to zipzipFile- File Target for zipping- Throws:
ZipExceptionIOException- 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 zipfilter- Allows monitoring each each zipped file or rejection- Returns:
- zipped file
- Throws:
ZipExceptionIOExceptionOperationAbortedException- 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 zipzipFile- File Target for zippingfilter- Allows monitoring each each zipped file or rejection. Throw OperationAbortedException to abort- Throws:
ZipExceptionIOExceptionOperationAbortedException- Since:
- 16
-
unzip
- Throws:
ZipExceptionIOException
-
unzip
- Throws:
ZipExceptionIOException
-
unzip
public static int unzip(ZipInputStream zis, File destDir, boolean downloading) throws ZipException, IOException - Throws:
ZipExceptionIOException
-