public class FileHandle extends Object
Files
instance.
Because some of the file types are backed by composite files and may be compressed (for example, if they are in an Android .apk
or are found via the classpath), the methods for extracting a path()
or file()
may not be appropriate for all
types. Use the Reader or Stream methods here to hide these dependencies from your platform independent code.Constructor and Description |
---|
FileHandle(File file)
Creates a new absolute FileHandle for the
File . |
FileHandle(String fileName)
Creates a new absolute FileHandle for the file name.
|
Modifier and Type | Method and Description |
---|---|
FileHandle |
child(String name)
Returns a handle to the child with the specified name.
|
void |
copyTo(FileHandle dest)
Copies this file or directory to the specified file or directory.
|
boolean |
delete()
Deletes this file or empty directory and returns success.
|
boolean |
deleteDirectory()
Deletes this file or directory and all children, recursively.
|
void |
emptyDirectory()
Deletes all children of this directory, recursively.
|
void |
emptyDirectory(boolean preserveTree)
Deletes all children of this directory, recursively.
|
boolean |
equals(Object obj) |
boolean |
exists()
Returns true if the file exists.
|
String |
extension() |
File |
file()
Returns a java.io.File that represents this file handle.
|
int |
hashCode() |
boolean |
isDirectory()
Returns true if this file is a directory.
|
long |
lastModified()
Returns the last modified time in milliseconds for this file.
|
long |
length()
Returns the length in bytes of this file, or 0 if this file is a directory, does not exist, or the size cannot otherwise be
determined.
|
FileHandle[] |
list()
Returns the paths to the children of this directory.
|
FileHandle[] |
list(FileFilter filter)
Returns the paths to the children of this directory that satisfy the specified filter.
|
FileHandle[] |
list(FilenameFilter filter)
Returns the paths to the children of this directory that satisfy the specified filter.
|
FileHandle[] |
list(String suffix)
Returns the paths to the children of this directory with the specified suffix.
|
void |
mkdirs() |
void |
moveTo(FileHandle dest)
Moves this file to the specified file, overwriting the file if it already exists.
|
String |
name() |
String |
nameWithoutExtension() |
FileHandle |
parent() |
String |
path() |
String |
pathWithoutExtension() |
InputStream |
read()
Returns a stream for reading this file as bytes.
|
BufferedInputStream |
read(int bufferSize)
Returns a buffered stream for reading this file as bytes.
|
byte[] |
readBytes()
Reads the entire file into a byte array.
|
int |
readBytes(byte[] bytes,
int offset,
int size)
Reads the entire file into the byte array.
|
Reader |
reader()
Returns a reader for reading this file as characters.
|
BufferedReader |
reader(int bufferSize)
Returns a buffered reader for reading this file as characters.
|
BufferedReader |
reader(int bufferSize,
String charset)
Returns a buffered reader for reading this file as characters.
|
Reader |
reader(String charset)
Returns a reader for reading this file as characters.
|
String |
readString()
Reads the entire file into a string using the platform's default charset.
|
String |
readString(String charset)
Reads the entire file into a string using the specified charset.
|
FileHandle |
sibling(String name)
Returns a handle to the sibling with the specified name.
|
static FileHandle |
tempDirectory(String prefix) |
static FileHandle |
tempFile(String prefix) |
String |
toString() |
Files.FileType |
type() |
OutputStream |
write(boolean append)
Returns a stream for writing to this file.
|
OutputStream |
write(boolean append,
int bufferSize)
Returns a buffered stream for writing to this file.
|
void |
write(InputStream input,
boolean append)
Reads the remaining bytes from the specified stream and writes them to this file.
|
void |
writeBytes(byte[] bytes,
boolean append)
Writes the specified bytes to the file.
|
void |
writeBytes(byte[] bytes,
int offset,
int length,
boolean append)
Writes the specified bytes to the file.
|
Writer |
writer(boolean append)
Returns a writer for writing to this file using the default charset.
|
Writer |
writer(boolean append,
String charset)
Returns a writer for writing to this file.
|
void |
writeString(String string,
boolean append)
Writes the specified string to the file using the default charset.
|
void |
writeString(String string,
boolean append,
String charset)
Writes the specified string to the file using the specified charset.
|
public FileHandle(String fileName)
Files
interface instead.fileName
- the filename.public String path()
public String name()
public String extension()
public String nameWithoutExtension()
public String pathWithoutExtension()
public Files.FileType type()
public File file()
Files.FileType.Absolute
and Files.FileType.External
file handles.public InputStream read()
GdxRuntimeException
- if the file handle represents a directory, doesn't exist, or could not be read.public BufferedInputStream read(int bufferSize)
GdxRuntimeException
- if the file handle represents a directory, doesn't exist, or could not be read.public Reader reader()
GdxRuntimeException
- if the file handle represents a directory, doesn't exist, or could not be read.public Reader reader(String charset)
GdxRuntimeException
- if the file handle represents a directory, doesn't exist, or could not be read.public BufferedReader reader(int bufferSize)
GdxRuntimeException
- if the file handle represents a directory, doesn't exist, or could not be read.public BufferedReader reader(int bufferSize, String charset)
GdxRuntimeException
- if the file handle represents a directory, doesn't exist, or could not be read.public String readString()
GdxRuntimeException
- if the file handle represents a directory, doesn't exist, or could not be read.public String readString(String charset)
charset
- If null the default charset is used.GdxRuntimeException
- if the file handle represents a directory, doesn't exist, or could not be read.public byte[] readBytes()
GdxRuntimeException
- if the file handle represents a directory, doesn't exist, or could not be read.public int readBytes(byte[] bytes, int offset, int size)
bytes
- the array to load the file intooffset
- the offset to start writing bytessize
- the number of bytes to read, see length()
public OutputStream write(boolean append)
append
- If false, this file will be overwritten if it exists, otherwise it will be appended.GdxRuntimeException
- if this file handle represents a directory, if it is a Files.FileType.Classpath
or
Files.FileType.Internal
file, or if it could not be written.public OutputStream write(boolean append, int bufferSize)
append
- If false, this file will be overwritten if it exists, otherwise it will be appended.bufferSize
- The size of the buffer.GdxRuntimeException
- if this file handle represents a directory, if it is a Files.FileType.Classpath
or
Files.FileType.Internal
file, or if it could not be written.public void write(InputStream input, boolean append)
append
- If false, this file will be overwritten if it exists, otherwise it will be appended.GdxRuntimeException
- if this file handle represents a directory, if it is a Files.FileType.Classpath
or
Files.FileType.Internal
file, or if it could not be written.public Writer writer(boolean append)
append
- If false, this file will be overwritten if it exists, otherwise it will be appended.GdxRuntimeException
- if this file handle represents a directory, if it is a Files.FileType.Classpath
or
Files.FileType.Internal
file, or if it could not be written.public Writer writer(boolean append, String charset)
append
- If false, this file will be overwritten if it exists, otherwise it will be appended.charset
- May be null to use the default charset.GdxRuntimeException
- if this file handle represents a directory, if it is a Files.FileType.Classpath
or
Files.FileType.Internal
file, or if it could not be written.public void writeString(String string, boolean append)
append
- If false, this file will be overwritten if it exists, otherwise it will be appended.GdxRuntimeException
- if this file handle represents a directory, if it is a Files.FileType.Classpath
or
Files.FileType.Internal
file, or if it could not be written.public void writeString(String string, boolean append, String charset)
append
- If false, this file will be overwritten if it exists, otherwise it will be appended.charset
- May be null to use the default charset.GdxRuntimeException
- if this file handle represents a directory, if it is a Files.FileType.Classpath
or
Files.FileType.Internal
file, or if it could not be written.public void writeBytes(byte[] bytes, boolean append)
append
- If false, this file will be overwritten if it exists, otherwise it will be appended.GdxRuntimeException
- if this file handle represents a directory, if it is a Files.FileType.Classpath
or
Files.FileType.Internal
file, or if it could not be written.public void writeBytes(byte[] bytes, int offset, int length, boolean append)
append
- If false, this file will be overwritten if it exists, otherwise it will be appended.GdxRuntimeException
- if this file handle represents a directory, if it is a Files.FileType.Classpath
or
Files.FileType.Internal
file, or if it could not be written.public FileHandle[] list()
Files.FileType.Internal
handle to a directory on the classpath will return a zero length
array.GdxRuntimeException
- if this file is an Files.FileType.Classpath
file.public FileHandle[] list(FileFilter filter)
Files.FileType.Internal
handle to a directory on the
classpath will return a zero length array.filter
- the FileFilter
to filter filesGdxRuntimeException
- if this file is an Files.FileType.Classpath
file.public FileHandle[] list(FilenameFilter filter)
Files.FileType.Internal
handle to a directory on the
classpath will return a zero length array.filter
- the FilenameFilter
to filter filesGdxRuntimeException
- if this file is an Files.FileType.Classpath
file.public FileHandle[] list(String suffix)
Files.FileType.Internal
handle to a directory on the classpath
will return a zero length array.GdxRuntimeException
- if this file is an Files.FileType.Classpath
file.public boolean isDirectory()
Files.FileType.Internal
handle to an empty directory will return false. On the desktop, an Files.FileType.Internal
handle to a directory on the
classpath will return false.public FileHandle child(String name)
public FileHandle sibling(String name)
GdxRuntimeException
- if this file is the root.public FileHandle parent()
public void mkdirs()
GdxRuntimeException
- if this file handle is a Files.FileType.Classpath
or Files.FileType.Internal
file.public boolean exists()
Files.FileType.Classpath
or Files.FileType.Internal
handle to a directory
will always return false. Note that this can be very slow for internal files on Android!public boolean delete()
GdxRuntimeException
- if this file handle is a Files.FileType.Classpath
or Files.FileType.Internal
file.public boolean deleteDirectory()
GdxRuntimeException
- if this file handle is a Files.FileType.Classpath
or Files.FileType.Internal
file.public void emptyDirectory()
GdxRuntimeException
- if this file handle is a Files.FileType.Classpath
or Files.FileType.Internal
file.public void emptyDirectory(boolean preserveTree)
GdxRuntimeException
- if this file handle is a Files.FileType.Classpath
or Files.FileType.Internal
file.public void copyTo(FileHandle dest)
mkdirs()
is called on the destination's parent and this file is copied into it with a new name. If
this handle is a directory, then 1) if the destination is a file, GdxRuntimeException is thrown, or 2) if the destination is
a directory, this directory is copied into it recursively, overwriting existing files, or 3) if the destination doesn't
exist, mkdirs()
is called on the destination and this directory is copied into it recursively.GdxRuntimeException
- if the destination file handle is a Files.FileType.Classpath
or Files.FileType.Internal
file, or copying failed.public void moveTo(FileHandle dest)
GdxRuntimeException
- if the source or destination file handle is a Files.FileType.Classpath
or
Files.FileType.Internal
file.public long length()
public long lastModified()
Files.FileType.Classpath
files. On Android, zero is returned for Files.FileType.Internal
files. On the desktop, zero
is returned for Files.FileType.Internal
files on the classpath.public static FileHandle tempFile(String prefix)
public static FileHandle tempDirectory(String prefix)
Copyright © 2015. All rights reserved.