File Class
Used to represent a single file on a file system. In many ways, this class
is an extension of the java.io.File class. However, unlike the java.io.File
class, this object provides functions that are relevant and specific to
files (not directories).
Constructors
public File( String Path )
public File( java.io.File File )
public File( java.io.File Parent, String Child )
public File( javaxt.io.Directory Parent, String Child )
public File( String Parent, String Child )
Methods
checksum( ) returns long
Returns a long value representing a cyclic redundancy check
(CRC-32 checksum) of the file, or -1 if not known.
clone( ) returns javaxt.io.File
Creates a copy of this object.
compareTo( Object obj ) returns int
copyTo( javaxt.io.Directory Destination, boolean Overwrite ) returns boolean
Used to create a copy of this file. Preserves the last modified date
associated with the source file. Returns true if the file was copied
successfully.
copyTo( javaxt.io.File Destination, boolean Overwrite ) returns boolean
Used to create a copy of this file. Preserves the last modified date
associated with the source file. Returns true if the file was copied
successfully.
create( ) returns void
Creates a new file if one does not exist.
delete( ) returns boolean
Used to delete the file. Warning: this operation is irrecoverable.
equals( Object obj ) returns boolean
exists( ) returns boolean
Used to determine whether a file exists. Returns false if the file
system can't find the file or if the object is a directory.
getBufferedImage( ) returns java.awt.image.BufferedImage
getBufferedReader( ) returns java.io.BufferedReader
Used to extract the contents of the file into a BufferedReader.
BufferedReader br = file.getBufferedReader("UTF-8");
String strLine;
while ((strLine = br.readLine()) != null){
System.out.println(strLine);
}
getBufferedReader( String charsetName ) returns java.io.BufferedReader
Used to extract the contents of the file into a BufferedReader.
BufferedReader br = file.getBufferedReader("UTF-8");
String strLine;
while ((strLine = br.readLine()) != null){
System.out.println (strLine);
}
WARNING: This method will never throw an error.
charsetName | Name of the character encoding used to read the file. Examples include UTF-8 and ISO-8859-1 |
getBufferedWriter( String charsetName ) returns java.io.BufferedWriter
Used to instantiate a BufferedWriter for this file.
getBytes( ) returns java.io.ByteArrayOutputStream
Returns a ByteArrayOutputStream for the file. Returns a null if a
ByteArrayOutputStream cannot be created (e.g. file does not exist).
getContentType( ) returns String
Returns the mime type associated with the file extension. This method
only covers the most common/popular mime types. The returned mime type
is NOT authoritative.
getContentType( String file ) returns String
Returns the mime type associated with the file extension found in a
given file name. This method only covers the most common/popular mime
types. The returned mime type is NOT authoritative.
file | File name (e.g. hello.txt) |
getCreationTime( ) returns java.util.Date
Returns a timestamp of when the file was first created. Returns a null
if the timestamp is not available.
getDate( ) returns java.util.Date
Returns a timestamp of when the file was last modified. Returns null if
the file does not exist or if the object is a directory.
getDirectory( ) returns javaxt.io.Directory
Returns the file's parent directory. Same as getParentDirectory()
getExtension( ) returns String
Returns the file's extension, excluding the last dot/period
(e.g. "C:\image.jpg" will return "jpg"). Returns a zero-length string
if there is no extension.
getFlags( ) returns java.util.HashSet
Returns keywords representing file attributes (e.g. "READONLY", "HIDDEN",
etc).
getImage( ) returns javaxt.io.Image
Used to open the file and read the contents into an image.
getInputStream( ) returns java.io.FileInputStream
Returns a new FileInputStream Object
getLastAccessTime( ) returns java.util.Date
Returns a timestamp of when the file was last accessed. Returns a null
if the timestamp is not available.
getLastModifiedTime( ) returns java.util.Date
Returns a timestamp of when the file was last modified. This is
identical to the getDate() method.
getLink( ) returns java.io.File
Returns the target of a symbolic link, Windows junction, or Windows
shortcut.
getMD5( ) returns String
Returns a string representing the MD5 hash for the file.
getName( ) returns String
Returns the name of the file, excluding the path.
getName( boolean IncludeFileExtension ) returns String
Returns the name of the file, excluding the path.
IncludeFileExtension | If true, includes the file extension. Otherwise, will return the file name without the extension. |
getOutputStream( ) returns java.io.FileOutputStream
Returns a new FileOutputStream Object
getParentDirectory( ) returns javaxt.io.Directory
Returns the file's parent directory. Returns null if the parent
directory is unknown.
getPath( ) returns String
Used to retrieve the path to the file, excluding the file name. Appends
a file separator to the end of the string.
getSHA1( ) returns String
Returns a string representing the SHA-1 hash for the file.
getSize( ) returns long
Returns the size of the file, in bytes. Returns 0L if the file is does
not exist or if the object is a directory.
getText( ) returns String
Used to open the file and read the contents into a string.
getText( String charsetName ) returns String
Used to extract the contents of the file as a String. Returns an empty
String if the file is empty or the contents cannot be converted to a
String.
charsetName | Name of the character encoding used to read the file. Examples include UTF-8 and ISO-8859-1 |
getXML( ) returns org.w3c.dom.Document
Returns an XML DOM Document (org.w3c.dom.Document) represented by this
file. Returns a null if the file contents cannot be converted into a
DOM Document.
hashCode( ) returns int
isExecutable( ) returns boolean
Used to check whether the file has execute permissions. Note that this
method is not supported by JDK 1.5 or lower. Instead, the method will
return false.
isHidden( ) returns boolean
Used to check whether the file is hidden. Returns true if the file
exists and is hidden according to the conventions of the underlying
file system.
isLink( ) returns boolean
Used to determine whether the file is actually a link to another file.
Returns true for symbolic links, Windows junctions, and Windows
shortcuts.
isReadOnly( ) returns boolean
Used to check whether the file has write permissions. Returns true if
the file exists and the application is not allowed to write to the file.
MapPath( String RelPath ) returns String
moveTo( javaxt.io.Directory Destination ) returns javaxt.io.File
Used to move the file to a different directory. If the operation is
successful, returns a handle to the new file. Otherwise, the original
file is returned.
moveTo( javaxt.io.File Destination, boolean Overwrite ) returns javaxt.io.File
Used to move the file to a different location. If the operation is
successful, returns a handle to the new file. Otherwise, the original
file is returned.
rename( String FileName ) returns javaxt.io.File
Used to rename the file. The existing file name is replaced with a new
name. Only the file name is affected. The file path remains unchanged.
This method is therefore different from the java.io.File "renameTo"
method. If the operation is successful, returns a handle to the new file.
Otherwise, the original file is returned.
FileName | The new file name (including the file extension). |
setBufferSize( int numBytes ) returns void
Used to set the size of the buffer used to read/write bytes. The default
is 1MB (1,048,576 bytes)
setDate( java.util.Date lastModified ) returns boolean
Used to set/update the last modified date.
setLastModifiedTime( java.util.Date date ) returns void
Used to update the timestamp of when the file was last modified.
toFile( ) returns java.io.File
Returns the java.io.File representation of this object.
toString( ) returns String
Returns the full file path (including the file name)
write( java.io.ByteArrayOutputStream bas ) returns void
Creates a new file using the given ByteArrayOutputStream. Note that this
method will convert the ByteArrayOutputStream to a byte array using the
toByteArray() method which may result in memory issues.
write( byte[] bytes ) returns void
Creates a new file using the given byte array.
write( java.io.InputStream input ) returns void
Creates a new file using the given InputStream. Note that the caller is
responsible for closing the input stream after the method is complete.
write( String Text ) returns void
Used to write text to a file. Uses UTF-8 character encoding. Use the
other write method to specify a different character encoding (e.g.
ISO-8859-1).
write( org.w3c.dom.Document xml ) returns void
Used to write an XML DOM Document to a file.
write( String[] Content ) returns void
Used to write text to a file using an array of strings. A new line is
created for each entry in the array.
write( java.awt.image.BufferedImage Image ) returns void
Used to write an image to a file.
write( String Text, String charsetName ) returns void
Used to write text to a file. Allows users to specify character encoding.
Text | |
charsetName | Name of the character encoding used to read the file. Examples include UTF-8 and ISO-8859-1. If null, the writer will use the default character encoding defined on the host machine. |