se.datadosen.util
Class IO

java.lang.Object
  extended by se.datadosen.util.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
static java.lang.String reserved
           
static java.lang.String rfc3986Reserved
          Performs encoding similar to classic URL encoding but also encodes accented characters and encodes space to %20 (Netscape in mac likes this) Seems to be neccesary for the part of an URL that is before the ?
 
Constructor Summary
IO()
           
 
Method Summary
static java.lang.String baseName(java.io.File file)
           
static java.lang.String baseName(java.lang.String fullName)
           
static void close(java.io.FileOutputStream fos)
           
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 nomatter how each part looks.
static void copyDirectoryContent(java.io.File srcDir, java.io.File dest, boolean forceCopy)
           
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 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 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 boolean deleteDir(java.io.File dir)
          The very powerful (and dangerous) delete directory Will attempt to move the directory to the recycle bin first (not supported on Mac)
static boolean deleteDir(java.io.File dir, boolean recycleOnly)
          The very powerful (and dangerous) delete directory Will attempt to move the directory to the recycle bin first (not supported on Mac)
static java.lang.String extensionOf(java.io.File file)
           
static java.lang.String extensionOf(java.lang.String name)
           
static boolean isSubdirectoryOf(java.io.File subdir, java.io.File dir)
          Test if subdir is a subdirectory or dir (or one of its subdirectories recursively)
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)
           
static java.lang.String readTextUrl(java.net.URL textUrl)
           
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 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 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 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)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

rfc3986Reserved

public static final java.lang.String rfc3986Reserved
Performs encoding similar to classic URL encoding but also encodes accented characters and encodes space to %20 (Netscape in mac likes this) Seems to be neccesary for the part of an URL that is before the ?

See Also:
Constant Field Values

reserved

public static final java.lang.String reserved
See Also:
Constant Field Values
Constructor Detail

IO

public IO()
Method Detail

readTextFile

public static java.lang.String readTextFile(java.io.File file,
                                            java.lang.String encoding)
                                     throws java.io.IOException
Read a text file into a String. Autodetects UTF-8 and UTF-16 encodings

Parameters:
file -
encoding -
Returns:
Throws:
java.io.IOException

readTextFile

public static java.lang.String readTextFile(java.io.File file)
                                     throws java.io.IOException
Throws:
java.io.IOException

readTextFile

public static java.lang.String readTextFile(java.lang.String fileName)
                                     throws java.io.IOException
Throws:
java.io.IOException

readTextUrl

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

readBytes

public static byte[] readBytes(java.io.File f)
                        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

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. It 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

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 or 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

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

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

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


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 nomatter 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.FileOutputStream fos)

deleteDir

public static boolean deleteDir(java.io.File dir)
The very powerful (and dangerous) delete directory Will attempt to move the directory to the recycle bin first (not supported on Mac)

Parameters:
dir - Directory to delete
Returns:
true if directory was recycled instead of deleted
Since:
8.5

deleteDir

public static boolean deleteDir(java.io.File dir,
                                boolean recycleOnly)
The very powerful (and dangerous) delete directory Will attempt to move the directory to the recycle bin first (not supported on Mac)

Parameters:
dir - Directory to delete
Returns:
true if directory was recycled instead of deleted
Since:
8.5