org.h2.store.fs
Class FileUtils

java.lang.Object
  extended by org.h2.store.fs.FileUtils

public class FileUtils
extends java.lang.Object

This utility class contains utility functions that use the file system abstraction.


Constructor Summary
FileUtils()
           
 
Method Summary
static boolean canWrite(java.lang.String fileName)
          Check if the file is writable.
static void copy(java.lang.String original, java.lang.String copy)
          Copy a file from one directory to another, or to another file.
static void createDirectories(java.lang.String dir)
          Create the directory and all required parent directories.
static void createDirectory(java.lang.String directoryName)
          Create a directory (all required parent directories must already exist).
static boolean createFile(java.lang.String fileName)
          Create a new 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 path)
          Delete a file or directory if it exists.
static void deleteRecursive(java.lang.String path, boolean tryOnly)
          Delete a directory or file and all subdirectories and files.
static boolean exists(java.lang.String fileName)
          Checks if a file exists.
static java.lang.String getName(java.lang.String path)
          Get the file or directory name (the last element of the path).
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 long lastModified(java.lang.String fileName)
          Get the last modified date of a file.
static void moveTo(java.lang.String oldName, java.lang.String newName)
          Rename a file if this is allowed.
static java.util.List<java.lang.String> newDirectoryStream(java.lang.String path)
          List the files and directories in the given directory.
static java.io.InputStream newInputStream(java.lang.String fileName)
          Create an input stream to read from the file.
static java.io.OutputStream newOutputStream(java.lang.String fileName, boolean append)
          Create an output stream to write into the file.
static java.nio.channels.FileChannel open(java.lang.String fileName, java.lang.String mode)
          Open a random access file object.
static void readFully(java.nio.channels.FileChannel channel, java.nio.ByteBuffer dst)
          Fully read from the file.
static boolean setReadOnly(java.lang.String fileName)
          Disable the ability to write.
static long size(java.lang.String fileName)
          Get the size of a file in bytes This method is similar to Java 7 java.nio.file.attribute.Attributes.
static java.lang.String toRealPath(java.lang.String fileName)
          Get the canonical file or directory name.
static boolean tryDelete(java.lang.String fileName)
          Try to delete a file (ignore errors).
static java.lang.String unwrap(java.lang.String fileName)
          Get the unwrapped file name (without wrapper prefixes if wrapping / delegating file systems are used).
static void writeFully(java.nio.channels.FileChannel channel, java.nio.ByteBuffer src)
          Fully write to the file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtils

public FileUtils()
Method Detail

exists

public static boolean exists(java.lang.String fileName)
Checks if a file exists. This method is similar to Java 7 java.nio.file.Path.exists.

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

createDirectory

public static void createDirectory(java.lang.String directoryName)
Create a directory (all required parent directories must already exist). This method is similar to Java 7 java.nio.file.Path.createDirectory.

Parameters:
directoryName - the directory name

createFile

public static boolean createFile(java.lang.String fileName)
Create a new file. This method is similar to Java 7 java.nio.file.Path.createFile, but returns false instead of throwing a exception if the file already existed.

Parameters:
fileName - the file name
Returns:
true if creating was successful

delete

public static void delete(java.lang.String path)
Delete a file or directory if it exists. Directories may only be deleted if they are empty. This method is similar to Java 7 java.nio.file.Path.deleteIfExists.

Parameters:
path - the file or directory name

toRealPath

public static java.lang.String toRealPath(java.lang.String fileName)
Get the canonical file or directory name. This method is similar to Java 7 java.nio.file.Path.toRealPath.

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

getParent

public static java.lang.String getParent(java.lang.String fileName)
Get the parent directory of a file or directory. This method returns null if there is no parent. This method is similar to Java 7 java.nio.file.Path.getParent.

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

isAbsolute

public static boolean isAbsolute(java.lang.String fileName)
Check if the file name includes a path. This method is similar to Java 7 java.nio.file.Path.isAbsolute.

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

moveTo

public static void moveTo(java.lang.String oldName,
                          java.lang.String newName)
Rename a file if this is allowed. This method is similar to Java 7 java.nio.file.Path.moveTo.

Parameters:
oldName - the old fully qualified file name
newName - the new fully qualified file name

getName

public static java.lang.String getName(java.lang.String path)
Get the file or directory name (the last element of the path). This method is similar to Java 7 java.nio.file.Path.getName.

Parameters:
path - the directory and file name
Returns:
just the file name

newDirectoryStream

public static java.util.List<java.lang.String> newDirectoryStream(java.lang.String path)
List the files and directories in the given directory. This method is similar to Java 7 java.nio.file.Path.newDirectoryStream.

Parameters:
path - the directory
Returns:
the list of fully qualified file names

lastModified

public static long lastModified(java.lang.String fileName)
Get the last modified date of a file. This method is similar to Java 7 java.nio.file.attribute.Attributes. readBasicFileAttributes(file).lastModified().toMillis()

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

size

public static long size(java.lang.String fileName)
Get the size of a file in bytes This method is similar to Java 7 java.nio.file.attribute.Attributes. readBasicFileAttributes(file).size()

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

isDirectory

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

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

open

public static java.nio.channels.FileChannel open(java.lang.String fileName,
                                                 java.lang.String mode)
                                          throws java.io.IOException
Open a random access file object. This method is similar to Java 7 java.nio.channels.FileChannel.open.

Parameters:
fileName - the file name
mode - the access mode. Supported are r, rw, rws, rwd
Returns:
the file object
Throws:
java.io.IOException

newInputStream

public static java.io.InputStream newInputStream(java.lang.String fileName)
                                          throws java.io.IOException
Create an input stream to read from the file. This method is similar to Java 7 java.nio.file.Path.newInputStream.

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

newOutputStream

public static java.io.OutputStream newOutputStream(java.lang.String fileName,
                                                   boolean append)
Create an output stream to write into the file. This method is similar to Java 7 java.nio.file.Path.newOutputStream.

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

canWrite

public static boolean canWrite(java.lang.String fileName)
Check if the file is writable. This method is similar to Java 7 java.nio.file.Path.checkAccess(AccessMode.WRITE)

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

setReadOnly

public static boolean setReadOnly(java.lang.String fileName)
Disable the ability to write. The file can still be deleted afterwards.

Parameters:
fileName - the file name
Returns:
true if the call was successful

unwrap

public static java.lang.String unwrap(java.lang.String fileName)
Get the unwrapped file name (without wrapper prefixes if wrapping / delegating file systems are used).

Parameters:
fileName - the file name
Returns:
the unwrapped

deleteRecursive

public static void deleteRecursive(java.lang.String path,
                                   boolean tryOnly)
Delete a directory or file and all subdirectories and files.

Parameters:
path - the path
tryOnly - whether errors should be ignored

createDirectories

public static void createDirectories(java.lang.String dir)
Create the directory and all required parent directories.

Parameters:
dir - the directory name

copy

public static void copy(java.lang.String original,
                        java.lang.String copy)
                 throws java.io.IOException
Copy a file from one directory to another, or to another file.

Parameters:
original - the original file name
copy - the file name of the copy
Throws:
java.io.IOException

tryDelete

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

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

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

readFully

public static void readFully(java.nio.channels.FileChannel channel,
                             java.nio.ByteBuffer dst)
                      throws java.io.IOException
Fully read from the file. This will read all remaining bytes, or throw an EOFException if not successful.

Parameters:
channel - the file channel
dst - the byte buffer
Throws:
java.io.IOException

writeFully

public static void writeFully(java.nio.channels.FileChannel channel,
                              java.nio.ByteBuffer src)
                       throws java.io.IOException
Fully write to the file. This will write all remaining bytes.

Parameters:
channel - the file channel
src - the byte buffer
Throws:
java.io.IOException