org.h2.util
Class FileUtils

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

public class FileUtils
extends java.lang.Object

This utility class supports basic operations on files


Method Summary
static void createDirs(java.lang.String fileName)
          Create all required directories that are required for this file.
static java.lang.String createTempFile(java.lang.String prefix, java.lang.String suffix, boolean deleteOnExit, boolean inTempDir)
          Create a new temporary file.
static void delete(java.lang.String fileName)
          Delete a file.
static boolean exists(java.lang.String fileName)
          Checks if a file exists.
static boolean fileStartsWith(java.lang.String fileName, java.lang.String prefix)
          Check if a file starts with a given prefix.
static java.lang.String getAbsolutePath(java.lang.String fileName)
          Get the absolute file name.
static java.lang.String getFileInUserHome(java.lang.String fileName)
          Get the absolute file path of a file in the user home directory.
static java.lang.String getFileName(java.lang.String name)
          Get the file name (without directory part).
static long getLastModified(java.lang.String fileName)
          Get the last modified date of a file.
static java.lang.String getParent(java.lang.String fileName)
          Get the parent directory of a file or directory.
static boolean isAbsolute(java.lang.String fileName)
          Check if the file name includes a path.
static boolean isDirectory(java.lang.String fileName)
          Check if it is a file or a directory.
static boolean isReadOnly(java.lang.String fileName)
          Check if a file is read-only.
static long length(java.lang.String fileName)
          Get the length of a file.
static java.lang.String[] listFiles(java.lang.String path)
          List the files in the given directory.
static void mkdirs(java.io.File directory)
          Create the directory and all parent directories if required.
static java.lang.String normalize(java.lang.String fileName)
          Normalize a file name.
static java.io.InputStream openFileInputStream(java.lang.String fileName)
          Create an input stream to read from the file.
static java.io.OutputStream openFileOutputStream(java.lang.String fileName, boolean append)
          Create an output stream to write into the file.
static void rename(java.lang.String oldName, java.lang.String newName)
          Rename a file if this is allowed.
static void setLength(java.io.RandomAccessFile file, long newLength)
          Change the length of the file.
static void setLength(java.lang.String fileName, long length)
          Set the file length.
static boolean tryDelete(java.lang.String fileName)
          Try to delete a file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

mkdirs

public static void mkdirs(java.io.File directory)
                   throws java.io.IOException
Create the directory and all parent directories if required.

Parameters:
directory - the directory
Throws:
java.io.IOException

setLength

public static void setLength(java.io.RandomAccessFile file,
                             long newLength)
                      throws java.io.IOException
Change the length of the file.

Parameters:
file - the random access file
newLength - the new length
Throws:
java.io.IOException

getFileInUserHome

public static java.lang.String getFileInUserHome(java.lang.String fileName)
Get the absolute file path of a file in the user home directory.

Parameters:
fileName - the file name
Returns:
the absolute path

getFileName

public static java.lang.String getFileName(java.lang.String name)
                                    throws java.sql.SQLException
Get the file name (without directory part).

Parameters:
name - the directory and file name
Returns:
just the file name
Throws:
java.sql.SQLException

normalize

public static java.lang.String normalize(java.lang.String fileName)
                                  throws java.sql.SQLException
Normalize a file name.

Parameters:
fileName - the file name
Returns:
the normalized file name
Throws:
java.sql.SQLException

tryDelete

public static boolean tryDelete(java.lang.String fileName)
Try to delete a file.

Parameters:
fileName - the file name
Returns:
true if it worked

isReadOnly

public static boolean isReadOnly(java.lang.String fileName)
Check if a file is read-only.

Parameters:
fileName - the file name
Returns:
if it is read only

exists

public static boolean exists(java.lang.String fileName)
Checks if a file exists.

Parameters:
fileName - the file name
Returns:
true if it exists

length

public static long length(java.lang.String fileName)
Get the length of a file.

Parameters:
fileName - the file name
Returns:
the length in bytes

createTempFile

public static java.lang.String createTempFile(java.lang.String prefix,
                                              java.lang.String suffix,
                                              boolean deleteOnExit,
                                              boolean inTempDir)
                                       throws java.io.IOException
Create a new temporary file.

Parameters:
prefix - the prefix of the file name (including directory name if required)
suffix - the suffix
deleteOnExit - if the file should be deleted when the virtual machine exists
inTempDir - if the file should be stored in the temporary directory
Returns:
the name of the created file
Throws:
java.io.IOException

getParent

public static java.lang.String getParent(java.lang.String fileName)
Get the parent directory of a file or directory.

Parameters:
fileName - the file or directory name
Returns:
the parent directory name

listFiles

public static java.lang.String[] listFiles(java.lang.String path)
                                    throws java.sql.SQLException
List the files in the given directory.

Parameters:
path - the directory
Returns:
the list of fully qualified file names
Throws:
java.sql.SQLException

isDirectory

public static boolean isDirectory(java.lang.String fileName)
Check if it is a file or a directory.

Parameters:
fileName - the file or directory name
Returns:
true if it is a directory

isAbsolute

public static boolean isAbsolute(java.lang.String fileName)
Check if the file name includes a path.

Parameters:
fileName - the file name
Returns:
if the file name is absolute

getAbsolutePath

public static java.lang.String getAbsolutePath(java.lang.String fileName)
Get the absolute file name.

Parameters:
fileName - the file name
Returns:
the absolute file name

fileStartsWith

public static boolean fileStartsWith(java.lang.String fileName,
                                     java.lang.String prefix)
Check if a file starts with a given prefix.

Parameters:
fileName - the complete file name
prefix - the prefix
Returns:
true if it starts with the prefix

openFileInputStream

public static java.io.InputStream openFileInputStream(java.lang.String fileName)
                                               throws java.io.IOException
Create an input stream to read from the file.

Parameters:
fileName - the file name
Returns:
the input stream
Throws:
java.io.IOException

openFileOutputStream

public static java.io.OutputStream openFileOutputStream(java.lang.String fileName,
                                                        boolean append)
                                                 throws java.sql.SQLException
Create an output stream to write into the file.

Parameters:
fileName - the file name
append - if true, the file will grow, if false, the file will be truncated first
Returns:
the output stream
Throws:
java.sql.SQLException

rename

public static void rename(java.lang.String oldName,
                          java.lang.String newName)
                   throws java.sql.SQLException
Rename a file if this is allowed.

Parameters:
oldName - the old fully qualified file name
newName - the new fully qualified file name
Throws:
java.sql.SQLException

createDirs

public static void createDirs(java.lang.String fileName)
                       throws java.sql.SQLException
Create all required directories that are required for this file.

Parameters:
fileName - the file name (not directory name)
Throws:
java.sql.SQLException

delete

public static void delete(java.lang.String fileName)
                   throws java.sql.SQLException
Delete a file.

Parameters:
fileName - the file name
Throws:
java.sql.SQLException

getLastModified

public static long getLastModified(java.lang.String fileName)
Get the last modified date of a file.

Parameters:
fileName - the file name
Returns:
the last modified date

setLength

public static void setLength(java.lang.String fileName,
                             long length)
                      throws java.sql.SQLException
Set the file length.

Parameters:
fileName - the file name
length - the new length
Throws:
java.sql.SQLException