Top Description Fields Constructors Methods
org.h2.store.fs

public Class FileUtils

extends Object
Class Inheritance
Imports
java.io.BufferedReader, .EOFException, .File, .IOException, .InputStream, .InputStreamReader, .OutputStream, java.nio.ByteBuffer, java.nio.channels.FileChannel, java.nio.charset.Charset, java.nio.file.OpenOption, .StandardOpenOption, java.nio.file.attribute.FileAttribute, java.util.ArrayList, .Collections, .EnumSet, .List, .Set, org.h2.engine.Constants

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

Field Summary

Modifier and TypeField and Description
public static final FileAttribute<?>[]
NO_ATTRIBUTES

No file attributes.

public static final Set<? extends OpenOption>
public static final Set<? extends OpenOption>
public static final Set<? extends OpenOption>
public static final Set<? extends OpenOption>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static boolean

Returns:

if the file is writable
canWrite
(String
the file name
fileName
)

Check if the file is writable.

public static void
createDirectories(String
the directory name
dir
)

Create the directory and all required parent directories.

public static void
createDirectory(String
the directory name
directoryName
)

Create a directory (all required parent directories must already exist).

public static boolean

Returns:

true if creating was successful
createFile
(String
the file name
fileName
)

Create a new file.

public static String

Returns:

the name of the created file
createTempFile
(String
the prefix of the file name (including directory name if required)
prefix
,
String
the suffix
suffix
,
boolean
if the file should be stored in the temporary directory
inTempDir
)

Create a new temporary file.

public static void
delete(String
the file or directory name
path
)

Delete a file or directory if it exists.

public static void
deleteRecursive(String
the path
path
,
boolean
whether errors should be ignored
tryOnly
)

Delete a directory or file and all subdirectories and files.

public static boolean

Returns:

true if it exists
exists
(String
the file name
fileName
)

Checks if a file exists.

public static String

Returns:

just the file name
getName
(String
the directory and file name
path
)

Get the file or directory name (the last element of the path).

public static String

Returns:

the parent directory name
getParent
(String
the file or directory name
fileName
)

Get the parent directory of a file or directory.

public static boolean

Returns:

if the file name is absolute
isAbsolute
(String
the file name
fileName
)

Check if the file name includes a path.

public static boolean

Returns:

true if it is a directory
isDirectory
(String
the file or directory name
fileName
)

Check if it is a file or a directory.

public static boolean

Returns:

true if it is a regular file
isRegularFile
(String
the file or directory name
fileName
)

Tests whether a file is a regular file.

public static long

Returns:

the last modified date
lastModified
(String
the file name
fileName
)

Get the last modified date of a file.

public static Set<? extends OpenOption>

Returns:

the set
modeToOptions
(String
the mode as a string
mode
)

Convert the string representation to a set.

public static void
move(String
the old fully qualified file name
source
,
String
the new fully qualified file name
target
)

Rename a file if this is allowed.

public static void
moveAtomicReplace(String
the old fully qualified file name
source
,
String
the new fully qualified file name
target
)

Rename a file if this is allowed, and try to atomically replace an existing file.

public static BufferedReader

Returns:

the buffered reader
newBufferedReader
(String
the file name
fileName
,
Charset
the charset
charset
)

Create a buffered reader to read from the file.

public static List<String>

Returns:

the list of fully qualified file names
newDirectoryStream
(String
the directory
path
)

List the files and directories in the given directory.

public static InputStream

Returns:

the input stream
newInputStream
(String
the file name
fileName
)

Create an input stream to read from the file.

public static OutputStream

Returns:

the output stream
newOutputStream
(String
the file name
fileName
,
boolean
if true, the file will grow, if false, the file will be truncated first
append
)

Create an output stream to write into the file.

public static FileChannel

Returns:

the file object
open
(String
the file name
fileName
,
String
the access mode. Supported are r, rw, rws, rwd
mode
)

Open a random access file object.

public static void
readFully(FileChannel
the file channel
channel
,
ByteBuffer
the byte buffer
dst
)

Fully read from the file.

public static boolean

Returns:

true if the call was successful
setReadOnly
(String
the file name
fileName
)

Disable the ability to write.

public static long

Returns:

the size in bytes
size
(String
the file name
fileName
)

Get the size of a file in bytes This method is similar to Java 7 java.nio.file.attribute.Attributes.

public static String

Returns:

the normalized file name
toRealPath
(String
the file name
fileName
)

Get the canonical file or directory name.

public static boolean

Returns:

true if it worked
tryDelete
(String
the file or directory name
path
)

Try to delete a file or directory (ignoring errors).

public static String

Returns:

the unwrapped
unwrap
(String
the file name
fileName
)

Get the unwrapped file name (without wrapper prefixes if wrapping / delegating file systems are used).

public static void
writeFully(FileChannel
the file channel
channel
,
ByteBuffer
the byte buffer
src
)

Fully write to the file.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

NO_ATTRIBUTESback to summary
public static final FileAttribute<?>[] NO_ATTRIBUTES

No file attributes.

Rback to summary
public static final Set<? extends OpenOption> R

StandardOpenOption#READ.

RWback to summary
public static final Set<? extends OpenOption> RW

StandardOpenOption#READ, StandardOpenOption#WRITE, and StandardOpenOption#CREATE.

RWDback to summary
public static final Set<? extends OpenOption> RWD

StandardOpenOption#READ, StandardOpenOption#WRITE, StandardOpenOption#CREATE, and StandardOpenOption#DSYNC.

RWSback to summary
public static final Set<? extends OpenOption> RWS

StandardOpenOption#READ, StandardOpenOption#WRITE, StandardOpenOption#CREATE, and StandardOpenOption#SYNC.

Constructor Detail

FileUtilsback to summary
public FileUtils()

Method Detail

canWriteback to summary
public static boolean canWrite(String fileName)

Check if the file is writable. This method is similar to Java 7 java.nio.file.Path.checkAccess(AccessMode.WRITE)

Parameters
fileName:String

the file name

Returns:boolean

if the file is writable

createDirectoriesback to summary
public static void createDirectories(String dir)

Create the directory and all required parent directories.

Parameters
dir:String

the directory name

createDirectoryback to summary
public static void createDirectory(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:String

the directory name

createFileback to summary
public static boolean createFile(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:String

the file name

Returns:boolean

true if creating was successful

createTempFileback to summary
public static String createTempFile(String prefix, String suffix, boolean inTempDir) throws IOException

Create a new temporary file.

Parameters
prefix:String

the prefix of the file name (including directory name if required)

suffix:String

the suffix

inTempDir:boolean

if the file should be stored in the temporary directory

Returns:String

the name of the created file

Exceptions
IOException:
on failure
deleteback to summary
public static void delete(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:String

the file or directory name

deleteRecursiveback to summary
public static void deleteRecursive(String path, boolean tryOnly)

Delete a directory or file and all subdirectories and files.

Parameters
path:String

the path

tryOnly:boolean

whether errors should be ignored

existsback to summary
public static boolean exists(String fileName)

Checks if a file exists. This method is similar to Java 7 java.nio.file.Path.exists.

Parameters
fileName:String

the file name

Returns:boolean

true if it exists

getNameback to summary
public static String getName(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:String

the directory and file name

Returns:String

just the file name

getParentback to summary
public static String getParent(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:String

the file or directory name

Returns:String

the parent directory name

isAbsoluteback to summary
public static boolean isAbsolute(String fileName)

Check if the file name includes a path. This method is similar to Java 7 java.nio.file.Path.isAbsolute.

Parameters
fileName:String

the file name

Returns:boolean

if the file name is absolute

isDirectoryback to summary
public static boolean isDirectory(String fileName)

Check if it is a file or a directory. java.nio.file.attribute.Attributes. readBasicFileAttributes(file).isDirectory()

Parameters
fileName:String

the file or directory name

Returns:boolean

true if it is a directory

isRegularFileback to summary
public static boolean isRegularFile(String fileName)

Tests whether a file is a regular file.

Parameters
fileName:String

the file or directory name

Returns:boolean

true if it is a regular file

lastModifiedback to summary
public static long lastModified(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:String

the file name

Returns:long

the last modified date

modeToOptionsback to summary
public static Set<? extends OpenOption> modeToOptions(String mode)

Convert the string representation to a set.

Parameters
mode:String

the mode as a string

Returns:Set<? extends OpenOption>

the set

moveback to summary
public static void move(String source, String target)

Rename a file if this is allowed. This method is similar to Java 7 java.nio.file.Files.move.

Parameters
source:String

the old fully qualified file name

target:String

the new fully qualified file name

moveAtomicReplaceback to summary
public static void moveAtomicReplace(String source, String target)

Rename a file if this is allowed, and try to atomically replace an existing file. This method is similar to Java 7 java.nio.file.Files.move.

Parameters
source:String

the old fully qualified file name

target:String

the new fully qualified file name

newBufferedReaderback to summary
public static BufferedReader newBufferedReader(String fileName, Charset charset) throws IOException

Create a buffered reader to read from the file. This method is similar to java.nio.file.Files.newBufferedReader().

Parameters
fileName:String

the file name

charset:Charset

the charset

Returns:BufferedReader

the buffered reader

Exceptions
IOException:
on failure
newDirectoryStreamback to summary
public static List<String> newDirectoryStream(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:String

the directory

Returns:List<String>

the list of fully qualified file names

newInputStreamback to summary
public static InputStream newInputStream(String fileName) throws IOException

Create an input stream to read from the file. This method is similar to Java 7 java.nio.file.Files.newInputStream().

Parameters
fileName:String

the file name

Returns:InputStream

the input stream

Exceptions
IOException:
on failure
newOutputStreamback to summary
public static OutputStream newOutputStream(String fileName, boolean append) throws IOException

Create an output stream to write into the file. This method is similar to java.nio.file.Files.newOutputStream().

Parameters
fileName:String

the file name

append:boolean

if true, the file will grow, if false, the file will be truncated first

Returns:OutputStream

the output stream

Exceptions
IOException:
on failure
openback to summary
public static FileChannel open(String fileName, String mode) throws IOException

Open a random access file object. This method is similar to Java 7 java.nio.channels.FileChannel.open.

Parameters
fileName:String

the file name

mode:String

the access mode. Supported are r, rw, rws, rwd

Returns:FileChannel

the file object

Exceptions
IOException:
on failure
readFullyback to summary
public static void readFully(FileChannel channel, ByteBuffer dst) throws IOException

Fully read from the file. This will read all remaining bytes, or throw an EOFException if not successful.

Parameters
channel:FileChannel

the file channel

dst:ByteBuffer

the byte buffer

Exceptions
IOException:
on failure
setReadOnlyback to summary
public static boolean setReadOnly(String fileName)

Disable the ability to write. The file can still be deleted afterwards.

Parameters
fileName:String

the file name

Returns:boolean

true if the call was successful

sizeback to summary
public static long size(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:String

the file name

Returns:long

the size in bytes

toRealPathback to summary
public static String toRealPath(String fileName)

Get the canonical file or directory name. This method is similar to Java 7 java.nio.file.Path.toRealPath.

Parameters
fileName:String

the file name

Returns:String

the normalized file name

tryDeleteback to summary
public static boolean tryDelete(String path)

Try to delete a file or directory (ignoring errors).

Parameters
path:String

the file or directory name

Returns:boolean

true if it worked

unwrapback to summary
public static String unwrap(String fileName)

Get the unwrapped file name (without wrapper prefixes if wrapping / delegating file systems are used).

Parameters
fileName:String

the file name

Returns:String

the unwrapped

writeFullyback to summary
public static void writeFully(FileChannel channel, ByteBuffer src) throws IOException

Fully write to the file. This will write all remaining bytes.

Parameters
channel:FileChannel

the file channel

src:ByteBuffer

the byte buffer

Exceptions
IOException:
on failure