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

File( String Path )
File( java.io.File File )
File( java.io.File Parent, String Child )
File( Directory Parent, String Child )
File( String Parent, String Child )

Properties

PathSeparator
LineSeperator

Public Methods

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.
IncludeFileExtensionIf true, includes the file extension. Otherwise, will return the file name without the extension.
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.
getDirectory( ) returns Directory
Returns the file's parent directory. Same as getParentDirectory()
getParentDirectory( ) returns Directory
Returns the file's parent directory. Returns null if the parent directory is unknown.
toFile( ) returns java.io.File
Returns the java.io.File representation of this object.
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.
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.
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.
setDate( java.util.Date lastModified ) returns boolean
Used to set/update the last modified date.
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.
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.
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.
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.
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.
getLink( ) returns java.io.File
Returns the target of a symbolic link, Windows junction, or Windows shortcut.
delete( ) returns boolean
Used to delete the file. Warning: this operation is irrecoverable.
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)
moveTo( 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.
copyTo( 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.
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.
FileNameThe new file name (including the file extension).
getBufferedWriter( String charsetName ) returns BufferedWriter
Used to instantiate a BufferedWriter for this file.
getBufferedReader( ) returns 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 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.
charsetNameName of the character encoding used to read the file. Examples include UTF-8 and ISO-8859-1
getBufferedImage( ) returns java.awt.image.BufferedImage
Returns the file contents as a BufferedImage. Returns a null if the file contents cannot be converted into a BufferedImage.
getImage( ) returns Image
Returns the file contents as an Image (javaxt.io.Image). Returns a null if the file contents cannot be converted into an Image.
getText( ) returns String
Returns the file contents as a String. Returns an empty String if the file is empty or the contents cannot be converted to a String.
getText( String charsetName ) returns String
Returns the file contents as a String. Returns an empty String if the file is empty or the contents cannot be converted to a String.
charsetNameName of the character encoding used to read the file. Examples include UTF-8 and ISO-8859-1
getXML( ) returns org.w3c.dom.Document
Returns the file contents as a XML DOM Document(org.w3c.dom.Document). Returns a null if the file contents cannot be converted into a DOM Document.
getJSONObject( ) returns javaxt.json.JSONObject
Returns the file contents as a JSON object (javaxt.json.JSONObject). Returns a null if the file contents cannot be converted into a JSON object.
getJSONArray( ) returns javaxt.json.JSONArray
Returns the file contents as a JSON array (javaxt.json.JSONArray). Returns a null if the file contents cannot be converted into a JSON array.
getBytes( ) returns ByteArrayOutputStream
Returns the file contents as a ByteArrayOutputStream. Returns a null if the file contents cannot be converted to a ByteArrayOutputStream.
checksum( ) returns long
Returns a long value representing a cyclic redundancy check (CRC-32 checksum) of the file, or -1 if not known.
getSHA1( ) returns String
Returns a string representing the SHA-1 hash for the file.
getMD5( ) returns String
Returns a string representing the MD5 hash for the file.
create( ) returns void
Creates a new file if one does not exist.
write( 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( 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( String Text, String charsetName ) returns void
Used to write text to a file. Allows users to specify character encoding.
Text
charsetNameName 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.
write( org.w3c.dom.Document xml ) returns void
Used to write an XML DOM Document to a file.
write( javaxt.json.JSONObject json ) returns void
Used to write a JSON object to a file. Note that the JSON object is encoded using UTF-8.
write( javaxt.json.JSONArray arr ) returns void
Used to write a JSON array to a file. Note that the JSON array is encoded using UTF-8.
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.
MapPath( String RelPath ) returns String
getInputStream( ) returns FileInputStream
Returns a new FileInputStream
getOutputStream( ) returns FileOutputStream
Returns a new FileOutputStream
toString( ) returns String
Returns the full file path (including the file name)
hashCode( ) returns int
compareTo( Object obj ) returns int
equals( Object obj ) returns boolean
clone( ) returns File
Creates a copy of this object.
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.
setLastModifiedTime( java.util.Date date ) returns void
Used to update the timestamp of when the file was last modified.
getLastModifiedTime( ) returns java.util.Date
Returns a timestamp of when the file was last modified. This is identical to the getDate() method.
getCreationTime( ) returns java.util.Date
Returns a timestamp of when the file was first created. Returns a null if the timestamp is not available.
getLastAccessTime( ) returns java.util.Date
Returns a timestamp of when the file was last accessed. Returns a null if the timestamp is not available.
getFlags( ) returns java.util.HashSet<String>
Returns keywords representing file attributes (e.g. "READONLY", "HIDDEN", etc).

Static Methods

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.
fileFile name (e.g. hello.txt)

Public Classes