org.jopendocument.util
Class FileUtils

java.lang.Object
  extended by org.jopendocument.util.FileUtils

public final class FileUtils
extends Object


Field Summary
static FileFilter DIR_FILTER
           
static StringUtils.Escaper FILENAME_ESCAPER
          An escaper suitable for producing valid filenames.
static Collection<Character> INVALID_CHARS
          Chars not valid in filenames.
static FileFilter REGULAR_FILE_FILTER
           
static Set<String> VersionControl
           
static String XML_TYPE
           
 
Method Summary
static File addSuffix(File f, String suffix)
           
static void browseFile(File f)
           
static void copyDirectory(File in, File out)
           
static void copyDirectory(File in, File out, Set<String> toIgnore)
           
static void copyDirectory(File in, File out, Set<String> toIgnore, boolean useTime)
           
static void copyFile(File in, File out)
           
static void copyFile(File in, File out, boolean useTime)
           
static void copyFile(File in, File out, long maxCount)
          Copy a file.
static FileFilter createEndFileFilter(String ext)
          Return a filter that select regular files ending in ext.
static File createTempDir(String prefix)
          Atomically creates a new directory somewhere beneath the system's temporary directory (as defined by the java.io.tmpdir system property), and returns its name.
static BufferedWriter createWriter(File f)
          Create an UTF-8 buffered writer.
static BufferedWriter createWriter(File f, Charset cs)
           
static BufferedWriter createXMLWriter(File f)
          Create a writer for the passed file, and write the XML declaration.
static
<T> T
doWithLock(File f, ExnTransformer<RandomAccessFile,T,?> transf)
          Execute the passed transformer with the lock on the passed file.
static String findMimeType(String fname)
          Try to guess the media type of the passed file name (see iana).
static List<File> getAncestors(File f)
           
static String getExtension(String fname)
          Return the string after the last dot.
static File getFile(URL url)
           
static List<File> list(File root, int depth)
           
static List<File> list(File root, int depth, FileFilter ff)
          Finds all files at the specified depth below root.
static List<File> list(File root, int minDepth, int maxDepth, FileFilter ff)
           
static List<File> list(File root, int minDepth, int maxDepth, FileFilter ff, boolean sort)
           
static List<String> listR(File dir)
          All the files (see File.isFile()) contained in the passed dir.
static List<String> listR(File dir, FileFilter ff)
           
static File ln(File target, File link)
          Create a symbolic link from link to target.
static File mkdir_p(File dir)
           
static File mkParentDirs(File f)
          Create all ancestors of f.
static String mv(File f, File dest)
          Behave like the 'mv' unix utility, ie handle cross filesystems mv and dest being a directory.
static File[] mvOut(File parent, String name, String suffix)
          Rename a file if necessary by finding a free name.
static void open(File f, String[] executables)
          Tries to open the passed file as if it were graphically opened by the current user (respect user's "open with").
static void openFile(File f)
           
static File prependSuffix(File f, String toInsert, String suffix)
          Prepend a string to a suffix.
static String read(File f)
          Read a file line by line with the default encoding and returns the concatenation of these.
static String read(File f, String charset)
          Read a file line by line and returns the concatenation of these.
static String read(InputStream ins, String charset)
           
static String read(Reader reader)
           
static String read(Reader reader, int bufferSize)
           
static byte[] readBytes(File f)
          Read the whole content of a file.
static File readlink(File link)
          Resolve a symbolic link or a windows shortcut.
static String relative(File fromDir, File to)
          Returns the relative path from one file to another in the same filesystem tree.
static String removeSuffix(String name, String suffix)
           
static void rm_R(File dir)
           
static void rm(File f)
           
static boolean rmR(File dir)
          Delete recursively the passed directory.
static void walk(File dir, IClosure<File> c)
           
static void walk(File dir, IClosure<File> c, RecursionType type)
           
static void write(String s, File f)
           
static void write(String s, File f, String charset, boolean append)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VersionControl

public static final Set<String> VersionControl

XML_TYPE

public static final String XML_TYPE
See Also:
Constant Field Values

INVALID_CHARS

public static final Collection<Character> INVALID_CHARS
Chars not valid in filenames.


FILENAME_ESCAPER

public static final StringUtils.Escaper FILENAME_ESCAPER
An escaper suitable for producing valid filenames.


DIR_FILTER

public static final FileFilter DIR_FILTER

REGULAR_FILE_FILTER

public static final FileFilter REGULAR_FILE_FILTER
Method Detail

browseFile

public static void browseFile(File f)

openFile

public static void openFile(File f)
                     throws IOException
Throws:
IOException

listR

public static List<String> listR(File dir)
All the files (see File.isFile()) contained in the passed dir.

Parameters:
dir - the root directory to search.
Returns:
a List of String.

listR

public static List<String> listR(File dir,
                                 FileFilter ff)

walk

public static void walk(File dir,
                        IClosure<File> c)

walk

public static void walk(File dir,
                        IClosure<File> c,
                        RecursionType type)

list

public static final List<File> list(File root,
                                    int depth)

list

public static final List<File> list(File root,
                                    int depth,
                                    FileFilter ff)
Finds all files at the specified depth below root.

Parameters:
root - the base directory
depth - the depth of the returned files.
ff - a filter, can be null.
Returns:
a list of files depth levels beneath root.

list

public static final List<File> list(File root,
                                    int minDepth,
                                    int maxDepth,
                                    FileFilter ff)

list

public static final List<File> list(File root,
                                    int minDepth,
                                    int maxDepth,
                                    FileFilter ff,
                                    boolean sort)

relative

public static final String relative(File fromDir,
                                    File to)
                             throws IOException
Returns the relative path from one file to another in the same filesystem tree. Files are not required to exist, see File.getCanonicalPath().

Parameters:
fromDir - the starting directory, eg /a/b/.
to - the file to get to, eg /a/x/y.txt.
Returns:
the relative path, eg "../x/y.txt".
Throws:
IOException - if an error occurs while canonicalizing the files.
IllegalArgumentException - if fromDir exists and is not directory.

getAncestors

public static final List<File> getAncestors(File f)

addSuffix

public static final File addSuffix(File f,
                                   String suffix)

prependSuffix

public static final File prependSuffix(File f,
                                       String toInsert,
                                       String suffix)
Prepend a string to a suffix.

Parameters:
f - the file, e.g. "sample.xml".
toInsert - the string to insert in the filename, e.g. "-sql".
suffix - the suffix of f, e.g. ".xml".
Returns:
a new file with toInsert prepended to suffix, e.g. "sample-sql.xml".

removeSuffix

public static final String removeSuffix(String name,
                                        String suffix)

mvOut

public static final File[] mvOut(File parent,
                                 String name,
                                 String suffix)
Rename a file if necessary by finding a free name. The tested names are name + "_" + i + suffix.

Parameters:
parent - the directory.
name - the base name of the file.
suffix - the suffix of the file, e.g. ".ods".
Returns:
new File(parent, name + suffix) (always non existing) and the new file, (or null if no file was moved).

mv

public static String mv(File f,
                        File dest)
Behave like the 'mv' unix utility, ie handle cross filesystems mv and dest being a directory.

Parameters:
f - the source file.
dest - the destination file or directory.
Returns:
the error or null if there was none.

copyFile

public static void copyFile(File in,
                            File out)
                     throws IOException
Throws:
IOException

copyFile

public static void copyFile(File in,
                            File out,
                            long maxCount)
                     throws IOException
Copy a file. It is generally not advised to use 0 for maxCount since various implementations have size limitations, see copyFile(File, File).

Parameters:
in - the source file.
out - the destination file.
maxCount - the number of bytes to copy at a time, 0 meaning size of in.
Throws:
IOException - if an error occurs.

copyFile

public static void copyFile(File in,
                            File out,
                            boolean useTime)
                     throws IOException
Throws:
IOException

copyDirectory

public static void copyDirectory(File in,
                                 File out)
                          throws IOException
Throws:
IOException

copyDirectory

public static void copyDirectory(File in,
                                 File out,
                                 Set<String> toIgnore)
                          throws IOException
Throws:
IOException

copyDirectory

public static void copyDirectory(File in,
                                 File out,
                                 Set<String> toIgnore,
                                 boolean useTime)
                          throws IOException
Throws:
IOException

rmR

public static boolean rmR(File dir)
Delete recursively the passed directory. If a deletion fails, the method stops attempting to delete and returns false.

Parameters:
dir - the dir to be deleted.
Returns:
true if all deletions were successful.

rm_R

public static void rm_R(File dir)
                 throws IOException
Throws:
IOException

rm

public static void rm(File f)
               throws IOException
Throws:
IOException

mkdir_p

public static final File mkdir_p(File dir)
                          throws IOException
Throws:
IOException

mkParentDirs

public static final File mkParentDirs(File f)
                               throws IOException
Create all ancestors of f.

Parameters:
f - any file whose ancestors should be created.
Returns:
f.
Throws:
IOException - if ancestors cannot be created.

read

public static final String read(File f)
                         throws IOException
Read a file line by line with the default encoding and returns the concatenation of these.

Parameters:
f - the file to read.
Returns:
the content of f.
Throws:
IOException - if a pb occur while reading.

read

public static final String read(File f,
                                String charset)
                         throws IOException
Read a file line by line and returns the concatenation of these.

Parameters:
f - the file to read.
charset - the encoding of f, null means default encoding.
Returns:
the content of f.
Throws:
IOException - if a pb occur while reading.

read

public static final String read(InputStream ins,
                                String charset)
                         throws IOException
Throws:
IOException

read

public static final String read(Reader reader)
                         throws IOException
Throws:
IOException

read

public static final String read(Reader reader,
                                int bufferSize)
                         throws IOException
Throws:
IOException

readBytes

public static final byte[] readBytes(File f)
                              throws IOException
Read the whole content of a file.

Parameters:
f - the file to read.
Returns:
its content.
Throws:
IOException - if a pb occur while reading.
IllegalArgumentException - if f is longer than Integer.MAX_VALUE.

write

public static void write(String s,
                         File f)
                  throws IOException
Throws:
IOException

write

public static void write(String s,
                         File f,
                         String charset,
                         boolean append)
                  throws IOException
Throws:
IOException

createXMLWriter

public static BufferedWriter createXMLWriter(File f)
                                      throws IOException
Create a writer for the passed file, and write the XML declaration.

Parameters:
f - a file
Returns:
a writer with the same encoding as the XML.
Throws:
IOException - if an error occurs.
See Also:
StreamUtils.createXMLWriter(java.io.OutputStream)

createWriter

public static BufferedWriter createWriter(File f)
                                   throws FileNotFoundException
Create an UTF-8 buffered writer.

Parameters:
f - the file to write to.
Returns:
a buffered writer.
Throws:
FileNotFoundException - if the file cannot be opened.

createWriter

public static BufferedWriter createWriter(File f,
                                          Charset cs)
                                   throws FileNotFoundException
Throws:
FileNotFoundException

doWithLock

public static final <T> T doWithLock(File f,
                                     ExnTransformer<RandomAccessFile,T,?> transf)
                          throws Exception
Execute the passed transformer with the lock on the passed file.

Type Parameters:
T - return type.
Parameters:
f - the file to lock.
transf - what to do on the file.
Returns:
what transf returns.
Throws:
Exception - if an error occurs.

getFile

public static final File getFile(URL url)
                          throws IOException
Throws:
IOException

ln

public static final File ln(File target,
                            File link)
                     throws IOException
Create a symbolic link from link to target.

Parameters:
target - the target of the link, eg ".".
link - the file to create or replace, eg "l".
Returns:
the link if the creation was successfull, null otherwise, eg "l.LNK".
Throws:
IOException - if an error occurs.

readlink

public static final File readlink(File link)
                           throws IOException
Resolve a symbolic link or a windows shortcut.

Parameters:
link - the shortcut, e.g. shortcut.lnk.
Returns:
the target of link, null if not found, e.g. target.txt.
Throws:
IOException - if an error occurs.

createTempDir

public static File createTempDir(String prefix)
Atomically creates a new directory somewhere beneath the system's temporary directory (as defined by the java.io.tmpdir system property), and returns its name.

Use this method instead of File.createTempFile(String, String) when you wish to create a directory, not a regular file. A common pitfall is to call createTempFile, delete the file and create a directory in its place, but this leads a race condition which can be exploited to create security vulnerabilities, especially when executable files are to be written into the directory.

This method assumes that the temporary volume is writable, has free inodes and free blocks, and that it will not be called thousands of times per second.

Parameters:
prefix - the prefix string to be used in generating the directory's name.
Returns:
the newly-created directory.
Throws:
IllegalStateException - if the directory could not be created.

open

public static final void open(File f,
                              String[] executables)
                       throws IOException
Tries to open the passed file as if it were graphically opened by the current user (respect user's "open with"). If a native way to open the file can't be found, tries the passed list of executables.

Parameters:
f - the file to open.
executables - a list of executables to try, e.g. ["ooffice", "soffice"].
Throws:
IOException - if the file can't be opened.

findMimeType

public static final String findMimeType(String fname)
Try to guess the media type of the passed file name (see iana).

Parameters:
fname - a file name.
Returns:
its mime type.

getExtension

public static final String getExtension(String fname)
Return the string after the last dot.

Parameters:
fname - a name, e.g. "test.odt" or "sans".
Returns:
the extension, e.g. "odt" or null.

createEndFileFilter

public static final FileFilter createEndFileFilter(String ext)
Return a filter that select regular files ending in ext.

Parameters:
ext - the end of the name, eg ".xml".
Returns:
the corresponding filter.


Copyright © 2010 jOpenDocument All Rights Reserved.