|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.jopendocument.util.FileUtils
public final class FileUtils
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
|
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 |
---|
public static final Set<String> VersionControl
public static final String XML_TYPE
public static final Collection<Character> INVALID_CHARS
public static final StringUtils.Escaper FILENAME_ESCAPER
public static final FileFilter DIR_FILTER
public static final FileFilter REGULAR_FILE_FILTER
Method Detail |
---|
public static void browseFile(File f)
public static void openFile(File f) throws IOException
IOException
public static List<String> listR(File dir)
File.isFile()
) contained in the passed dir.
dir
- the root directory to search.
public static List<String> listR(File dir, FileFilter ff)
public static void walk(File dir, IClosure<File> c)
public static void walk(File dir, IClosure<File> c, RecursionType type)
public static final List<File> list(File root, int depth)
public static final List<File> list(File root, int depth, FileFilter ff)
root
.
root
- the base directorydepth
- the depth of the returned files.ff
- a filter, can be null
.
depth
levels beneath root
.public static final List<File> list(File root, int minDepth, int maxDepth, FileFilter ff)
public static final List<File> list(File root, int minDepth, int maxDepth, FileFilter ff, boolean sort)
public static final String relative(File fromDir, File to) throws IOException
File.getCanonicalPath()
.
fromDir
- the starting directory, eg /a/b/.to
- the file to get to, eg /a/x/y.txt.
IOException
- if an error occurs while canonicalizing the files.
IllegalArgumentException
- if fromDir exists and is not directory.public static final List<File> getAncestors(File f)
public static final File addSuffix(File f, String suffix)
public static final File prependSuffix(File f, String toInsert, String suffix)
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".
toInsert
prepended to suffix
, e.g.
"sample-sql.xml".public static final String removeSuffix(String name, String suffix)
public static final File[] mvOut(File parent, String name, String suffix)
name + "_" + i + suffix
.
parent
- the directory.name
- the base name of the file.suffix
- the suffix of the file, e.g. ".ods".
new File(parent, name + suffix)
(always non existing) and the new file,
(or null
if no file was moved).public static String mv(File f, File dest)
dest
being
a directory.
f
- the source file.dest
- the destination file or directory.
null
if there was none.public static void copyFile(File in, File out) throws IOException
IOException
public static void copyFile(File in, File out, long maxCount) throws IOException
maxCount
since various
implementations have size limitations, see copyFile(File, File)
.
in
- the source file.out
- the destination file.maxCount
- the number of bytes to copy at a time, 0 meaning size of in
.
IOException
- if an error occurs.public static void copyFile(File in, File out, boolean useTime) throws IOException
IOException
public static void copyDirectory(File in, File out) throws IOException
IOException
public static void copyDirectory(File in, File out, Set<String> toIgnore) throws IOException
IOException
public static void copyDirectory(File in, File out, Set<String> toIgnore, boolean useTime) throws IOException
IOException
public static boolean rmR(File dir)
dir
- the dir to be deleted.
true
if all deletions were successful.public static void rm_R(File dir) throws IOException
IOException
public static void rm(File f) throws IOException
IOException
public static final File mkdir_p(File dir) throws IOException
IOException
public static final File mkParentDirs(File f) throws IOException
f
.
f
- any file whose ancestors should be created.
f
.
IOException
- if ancestors cannot be created.public static final String read(File f) throws IOException
f
- the file to read.
IOException
- if a pb occur while reading.public static final String read(File f, String charset) throws IOException
f
- the file to read.charset
- the encoding of f
, null
means default encoding.
IOException
- if a pb occur while reading.public static final String read(InputStream ins, String charset) throws IOException
IOException
public static final String read(Reader reader) throws IOException
IOException
public static final String read(Reader reader, int bufferSize) throws IOException
IOException
public static final byte[] readBytes(File f) throws IOException
f
- the file to read.
IOException
- if a pb occur while reading.
IllegalArgumentException
- if f is longer than Integer.MAX_VALUE
.public static void write(String s, File f) throws IOException
IOException
public static void write(String s, File f, String charset, boolean append) throws IOException
IOException
public static BufferedWriter createXMLWriter(File f) throws IOException
f
- a file
IOException
- if an error occurs.StreamUtils.createXMLWriter(java.io.OutputStream)
public static BufferedWriter createWriter(File f) throws FileNotFoundException
f
- the file to write to.
FileNotFoundException
- if the file cannot be opened.public static BufferedWriter createWriter(File f, Charset cs) throws FileNotFoundException
FileNotFoundException
public static final <T> T doWithLock(File f, ExnTransformer<RandomAccessFile,T,?> transf) throws Exception
T
- return type.f
- the file to lock.transf
- what to do on the file.
transf
returns.
Exception
- if an error occurs.public static final File getFile(URL url) throws IOException
IOException
public static final File ln(File target, File link) throws IOException
link
to target
.
target
- the target of the link, eg ".".link
- the file to create or replace, eg "l".
null
otherwise, eg "l.LNK".
IOException
- if an error occurs.public static final File readlink(File link) throws IOException
link
- the shortcut, e.g. shortcut.lnk.
link
, null
if not found, e.g. target.txt.
IOException
- if an error occurs.public static File createTempDir(String prefix)
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.
prefix
- the prefix string to be used in generating the directory's name.
IllegalStateException
- if the directory could not be created.public static final void open(File f, String[] executables) throws IOException
f
- the file to open.executables
- a list of executables to try, e.g. ["ooffice", "soffice"].
IOException
- if the file can't be opened.public static final String findMimeType(String fname)
fname
- a file name.
public static final String getExtension(String fname)
fname
- a name, e.g. "test.odt" or "sans".
null
.public static final FileFilter createEndFileFilter(String ext)
ext
.
ext
- the end of the name, eg ".xml".
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |