Package arc.files
Class Fi
java.lang.Object
arc.files.Fi
- Direct Known Subclasses:
AndroidFi
,IOSFi
,SdlFiles.SdlFi
,ZipFi
Represents a file or directory on the filesystem, classpath, Android SD card, or Android assets directory. FileHandles are
created via a
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.
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionReturns a handle to the child with the specified name.void
copyFilesTo
(Fi dest) Copies the contents of this folder into another folder.void
Copies this file or directory to the specified file or directory.boolean
delete()
Deletes this file or empty directory and returns success.boolean
Deletes this file or directory and all children, recursively.void
Deletes all children of this directory, recursively.void
emptyDirectory
(boolean preserveTree) Deletes all children of this directory, recursively.boolean
boolean
exists()
Returns true if the file exists.Returns the file extension (without the dot) or an empty string if the file name doesn't contain a dot.boolean
file()
Returns a java.io.File that represents this file handle.findAll()
Recursively iterates through all files in this directory and adds them to a newly allocated array.Recursively iterates through all files in this directory and adds them to an array.static Fi
int
hashCode()
boolean
Returns true if this file is a directory.long
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.Fi[]
list()
Returns the paths to the children of this directory.Fi[]
list
(FileFilter filter) Returns the paths to the children of this directory that satisfy the specified filter.Fi[]
list
(FilenameFilter filter) Returns the paths to the children of this directory that satisfy the specified filter.Fi[]
Returns the paths to the children of this directory with the specified suffix.map()
Attempts to memory map this file in READ_ONLY mode.map
(FileChannel.MapMode mode) Attempts to memory map this file.boolean
mkdirs()
void
Moves this file to the specified file, overwriting the file if it already exists.name()
parent()
path()
read()
Returns a stream for reading this file as bytes.read
(int bufferSize) Returns a buffered stream for reading this file as bytes.byte[]
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()
Returns a reader for reading this file as characters.reader
(int bufferSize) Returns a buffered reader for reading this file as characters.Returns a buffered reader for reading this file as characters.Returns a reader for reading this file as characters.reads()
Reads the entire file into a string using the platform's default charset.readString
(String charset) Reads the entire file into a string using the specified charset.seq()
Equivalent tolist()
, but returns a Seq.Returns a handle to the sibling with the specified name.static Fi
tempDirectory
(String prefix) static Fi
toString()
type()
void
Recursively iterates through all files in this directory.write()
write
(boolean append) Returns a stream for writing to this file.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) 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.void
writer
(boolean append) Returns a writer for writing to this file using the default charset.Returns a writer for writing to this file.writes()
writes
(boolean append) void
writeString
(String string) Writes a string without appending it.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.
-
Field Details
-
file
-
type
-
-
Constructor Details
-
Fi
protected Fi() -
Fi
Creates a new absolute FileHandle for the file name. Use this for tools on the desktop that don't need any of the backends. Do not use this constructor in case you write something cross-platform. Use theFiles
interface instead.- Parameters:
fileName
- the filename.
-
Fi
Creates a new absolute FileHandle for theFile
. Use this for tools on the desktop that don't need any of the backends. Do not use this constructor in case you write something cross-platform. Use theFiles
interface instead.- Parameters:
file
- the file.
-
Fi
-
Fi
-
-
Method Details
-
get
-
tempFile
-
tempDirectory
-
path
- Returns:
- the path of the file as specified on construction. Backward slashes will be replaced by forward slashes.
-
absolutePath
- Returns:
- the absolute path to this file without backslashes.
-
name
- Returns:
- the name of the file, without any parent paths.
-
extEquals
- Returns:
- whether this file's extension is equal to the specified string.
-
extension
Returns the file extension (without the dot) or an empty string if the file name doesn't contain a dot. -
nameWithoutExtension
- Returns:
- the name of the file, without parent paths or the extension.
-
pathWithoutExtension
- Returns:
- the path and filename without the extension, e.g. dir/dir2/file.png -> dir/dir2/file. backward slashes will be returned as forward slashes.
-
type
-
file
Returns a java.io.File that represents this file handle. Note the returned file will only be usable forFiles.absolute(java.lang.String)
andFiles.external(java.lang.String)
file handles. -
read
Returns a stream for reading this file as bytes.- Throws:
ArcRuntimeException
- if the file handle represents a directory, doesn't exist, or could not be read.
-
read
Returns a buffered stream for reading this file as bytes.- Throws:
ArcRuntimeException
- if the file handle represents a directory, doesn't exist, or could not be read.
-
reader
Returns a reader for reading this file as characters.- Throws:
ArcRuntimeException
- if the file handle represents a directory, doesn't exist, or could not be read.
-
reader
Returns a reader for reading this file as characters.- Throws:
ArcRuntimeException
- if the file handle represents a directory, doesn't exist, or could not be read.
-
reader
Returns a buffered reader for reading this file as characters.- Throws:
ArcRuntimeException
- if the file handle represents a directory, doesn't exist, or could not be read.
-
reader
Returns a buffered reader for reading this file as characters.- Throws:
ArcRuntimeException
- if the file handle represents a directory, doesn't exist, or could not be read.
-
readString
Reads the entire file into a string using the platform's default charset.- Throws:
ArcRuntimeException
- if the file handle represents a directory, doesn't exist, or could not be read.
-
readString
Reads the entire file into a string using the specified charset.- Parameters:
charset
- If null the default charset is used.- Throws:
ArcRuntimeException
- if the file handle represents a directory, doesn't exist, or could not be read.
-
readBytes
public byte[] readBytes()Reads the entire file into a byte array.- Throws:
ArcRuntimeException
- if the file handle represents a directory, doesn't exist, or could not be read.
-
readByteStream
- Returns:
- a new ByteArrayInputStream containing all the bytes in this file.
-
readBytes
public int readBytes(byte[] bytes, int offset, int size) Reads the entire file into the byte array. The byte array must be big enough to hold the file's data.- Parameters:
bytes
- the array to load the file intooffset
- the offset to start writing bytessize
- the number of bytes to read, seelength()
- Returns:
- the number of read bytes
-
map
Attempts to memory map this file in READ_ONLY mode. Android files must not be compressed.- Throws:
ArcRuntimeException
- if this file handle represents a directory, doesn't exist, or could not be read, or memory mapping fails, or is aFiles.classpath(java.lang.String)
file.
-
map
Attempts to memory map this file. Android files must not be compressed.- Throws:
ArcRuntimeException
- if this file handle represents a directory, doesn't exist, or could not be read, or memory mapping fails, or is aFiles.classpath(java.lang.String)
file.
-
writes
-
writes
-
reads
-
writesDeflate
-
readsDeflate
-
write
-
write
Returns a stream for writing to this file. Parent directories will be created if necessary.- Parameters:
append
- If false, this file will be overwritten if it exists, otherwise it will be appended.- Throws:
ArcRuntimeException
- if this file handle represents a directory, if it is aFiles.classpath(java.lang.String)
orFiles.internal(java.lang.String)
file, or if it could not be written.
-
write
Returns a buffered stream for writing to this file. Parent directories will be created if necessary.- Parameters:
append
- If false, this file will be overwritten if it exists, otherwise it will be appended.bufferSize
- The size of the buffer.- Throws:
ArcRuntimeException
- if this file handle represents a directory, if it is aFiles.classpath(java.lang.String)
orFiles.internal(java.lang.String)
file, or if it could not be written.
-
write
Reads the remaining bytes from the specified stream and writes them to this file. The stream is closed. Parent directories will be created if necessary.- Parameters:
append
- If false, this file will be overwritten if it exists, otherwise it will be appended.- Throws:
ArcRuntimeException
- if this file handle represents a directory, if it is aFiles.classpath(java.lang.String)
orFiles.internal(java.lang.String)
file, or if it could not be written.
-
writer
Returns a writer for writing to this file using the default charset. Parent directories will be created if necessary.- Parameters:
append
- If false, this file will be overwritten if it exists, otherwise it will be appended.- Throws:
ArcRuntimeException
- if this file handle represents a directory, if it is aFiles.classpath(java.lang.String)
orFiles.internal(java.lang.String)
file, or if it could not be written.
-
writer
Returns a writer for writing to this file. Parent directories will be created if necessary.- Parameters:
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.- Throws:
ArcRuntimeException
- if this file handle represents a directory, if it is aFiles.classpath(java.lang.String)
orFiles.internal(java.lang.String)
file, or if it could not be written.
-
writePng
-
writeString
Writes a string without appending it.- See Also:
-
writeString
Writes the specified string to the file using the default charset. Parent directories will be created if necessary.- Parameters:
append
- If false, this file will be overwritten if it exists, otherwise it will be appended.- Throws:
ArcRuntimeException
- if this file handle represents a directory, if it is aFiles.classpath(java.lang.String)
orFiles.internal(java.lang.String)
file, or if it could not be written.
-
writeString
Writes the specified string to the file using the specified charset. Parent directories will be created if necessary.- Parameters:
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.- Throws:
ArcRuntimeException
- if this file handle represents a directory, if it is aFiles.classpath(java.lang.String)
orFiles.internal(java.lang.String)
file, or if it could not be written.
-
writeBytes
public void writeBytes(byte[] bytes) -
writeBytes
public void writeBytes(byte[] bytes, boolean append) Writes the specified bytes to the file. Parent directories will be created if necessary.- Parameters:
append
- If false, this file will be overwritten if it exists, otherwise it will be appended.- Throws:
ArcRuntimeException
- if this file handle represents a directory, if it is aFiles.classpath(java.lang.String)
orFiles.internal(java.lang.String)
file, or if it could not be written.
-
writeBytes
public void writeBytes(byte[] bytes, int offset, int length, boolean append) Writes the specified bytes to the file. Parent directories will be created if necessary.- Parameters:
append
- If false, this file will be overwritten if it exists, otherwise it will be appended.- Throws:
ArcRuntimeException
- if this file handle represents a directory, if it is aFiles.classpath(java.lang.String)
orFiles.internal(java.lang.String)
file, or if it could not be written.
-
walk
Recursively iterates through all files in this directory. Directories are not handled. -
findAll
Recursively iterates through all files in this directory and adds them to an array. Directories are not handled. -
findAll
Recursively iterates through all files in this directory and adds them to a newly allocated array. -
seq
Equivalent tolist()
, but returns a Seq. -
list
Returns the paths to the children of this directory. Returns an empty list if this file handle represents a file and not a directory. On the desktop, anFiles.internal(java.lang.String)
handle to a directory on the classpath will return a zero length array.- Throws:
ArcRuntimeException
- if this file is anFiles.classpath(java.lang.String)
file.
-
list
Returns the paths to the children of this directory that satisfy the specified filter. Returns an empty list if this file handle represents a file and not a directory. On the desktop, anFiles.internal(java.lang.String)
handle to a directory on the classpath will return a zero length array.- Parameters:
filter
- theFileFilter
to filter files- Throws:
ArcRuntimeException
- if this file is anFiles.classpath(java.lang.String)
file.
-
list
Returns the paths to the children of this directory that satisfy the specified filter. Returns an empty list if this file handle represents a file and not a directory. On the desktop, anFiles.internal(java.lang.String)
handle to a directory on the classpath will return a zero length array.- Parameters:
filter
- theFilenameFilter
to filter files- Throws:
ArcRuntimeException
- if this file is anFiles.classpath(java.lang.String)
file.
-
list
Returns the paths to the children of this directory with the specified suffix. Returns an empty list if this file handle represents a file and not a directory. On the desktop, anFiles.internal(java.lang.String)
handle to a directory on the classpath will return a zero length array.- Throws:
ArcRuntimeException
- if this file is anFiles.classpath(java.lang.String)
file.
-
isDirectory
public boolean isDirectory()Returns true if this file is a directory. Always returns false for classpath files. On Android, anFiles.internal(java.lang.String)
handle to an empty directory will return false. On the desktop, anFiles.internal(java.lang.String)
handle to a directory on the classpath will return false. -
child
Returns a handle to the child with the specified name. -
sibling
Returns a handle to the sibling with the specified name.- Throws:
ArcRuntimeException
- if this file is the root.
-
parent
-
mkdirs
public boolean mkdirs()- Throws:
ArcRuntimeException
- if this file handle is aFiles.classpath(java.lang.String)
orFiles.internal(java.lang.String)
file.
-
exists
public boolean exists()Returns true if the file exists. On Android, aFiles.classpath(java.lang.String)
orFiles.internal(java.lang.String)
handle to a directory will always return false. Note that this can be very slow for internal files on Android! -
delete
public boolean delete()Deletes this file or empty directory and returns success. Will not delete a directory that has children.- Throws:
ArcRuntimeException
- if this file handle is aFiles.classpath(java.lang.String)
orFiles.internal(java.lang.String)
file.
-
deleteDirectory
public boolean deleteDirectory()Deletes this file or directory and all children, recursively.- Throws:
ArcRuntimeException
- if this file handle is aFiles.classpath(java.lang.String)
orFiles.internal(java.lang.String)
file.
-
emptyDirectory
public void emptyDirectory()Deletes all children of this directory, recursively.- Throws:
ArcRuntimeException
- if this file handle is aFiles.classpath(java.lang.String)
orFiles.internal(java.lang.String)
file.
-
emptyDirectory
public void emptyDirectory(boolean preserveTree) Deletes all children of this directory, recursively. Optionally preserving the folder structure.- Throws:
ArcRuntimeException
- if this file handle is aFiles.classpath(java.lang.String)
orFiles.internal(java.lang.String)
file.
-
copyTo
Copies this file or directory to the specified file or directory. If this handle is a file, then 1) if the destination is a file, it is overwritten, or 2) if the destination is a directory, this file is copied into it, or 3) if the destination doesn't exist,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, ArcRuntimeException 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.- Throws:
ArcRuntimeException
- if the destination file handle is aFiles.classpath(java.lang.String)
orFiles.internal(java.lang.String)
file, or copying failed.
-
copyFilesTo
Copies the contents of this folder into another folder. Unlike copyTo, this only copies the *contents*, not this folder itself.- Throws:
ArcRuntimeException
- if this or is not a valid directory, or copying fails.
-
moveTo
Moves this file to the specified file, overwriting the file if it already exists.- Throws:
ArcRuntimeException
- if the source or destination file handle is aFiles.classpath(java.lang.String)
orFiles.internal(java.lang.String)
file.
-
length
public 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. -
lastModified
public long lastModified()Returns the last modified time in milliseconds for this file. Zero is returned if the file doesn't exist. Zero is returned forFiles.classpath(java.lang.String)
files. On Android, zero is returned forFiles.internal(java.lang.String)
files. On the desktop, zero is returned forFiles.internal(java.lang.String)
files on the classpath. -
equals
-
hashCode
public int hashCode() -
toString
-