File Java Help


Although most of classes defined by operate on streams, the File class does not. It deals directly with files-and the file system. That is, the File Class does not specify how information is retrieved from or stored in files; it describes the properties of a file itself. A File object is used to obtain or manipulate tile information associated with a disk file, such as the permissions, time, date, and directory path, and to navigate sub directory hierarchies.

Files are a primary source and destination for data within many programs. Although there arc severe restrictions on their use within applets for security reasons, files are still a central resource for storing persistent and shared information. A directory in Java is treated simply as a File with one additional property-Alisa of file names that can be examined by the list() method.

The following constructors can be used to create File objects:

File(String directoryPath)
File(String directoryPath, String Filename)
File(File dirObj, String Filename)

Here, directory Path is the path name of the file,film time is the name of the file, and  dirObj is a File-object that specifics a directory.

The following example creates three files: fl, £2,and £3.The first File object is constructed with a directory path as the only argument. The second includes two argument  the path and the file name. The third includes the file path assigned to 11 and a filename; f3 refers to the same file as f2.

Java does the right thing tout path separators between UNIX ami Windowsill conventions. If you use nonroad slash, (/) on a Windows Recursion of Java, the pail: ‘Will still resolve (\)reply. Remember, if you are using tire Willdow~/DOS contention of a [,acks/asl, character (\), YOll will l1c to use its escape sequence ( ) to within string. The Java convention is to use tile UNIX- and URL style forward slash, for patel sculptors.

File defines many methods that obtain the standard properties of a File object. For example, getName( ) returns the name of the file, getparent ) returns the name of the parent directory, and exists( ) returns true if the file exists, false if it does not. The File class, however, is not symmetrical. By this, we mean that there are many methods that allow you to examine the properties of a simple file object, but no corresponding function exists to, change those attributes. The following example demonstrates several of the File methods.

Most of the File methods are self-explanatory. isFile( ) and isAbsolute( )·are not. bFile( ) returns true if called on a fill! and false if called on a directory. Also, isFlc( ) returns false for some special files, such as device drivers and named pipes, so this method can be used to make sure the file’ will behave as a file. The isAbsolute() method returns true if the file has an absolute path and false if its path is relative .

File also includes two useful utility methods. The first is renameTo( ), shown here:

boolean rename (File newName)

Here, the filename specified.Name becomes the new name of the invoking File object. It will return true upon success and false if the file cannot be renamed (if you either attempt ,to rename a file so that it moves from one directory to another or use an existing filename, for example).

The second utility method is delete C (), which deletes tile disk file represented by the path of the invoking File object. It is shown here:

boolean delete ( )

You can also use delete( ) to delete a directory if the directory is empty delete() returns true if it deletes the file and false if the file cannot be removed:

Java 2 adds some new methods to File that you might find helpful in certain situations. Some of the most interesting are shown here:

Method                                                                                           Description

void deleteOnExit( )                                                                           Removes the file associated with the invoking object when the Java Virtual
Machine terminates.

boolean isHidden( )                                                                          Return True if the invoking file is hidden. Returns false otherwise.

Boolean setlastModified (Along millisec)                                  Sets the timestamp on the invoking file to that specified by millisec, which
is the number of milliseconds from January 1, 1970, Coordinated Universal                                                                                                                      Time (UTC).

boolean setReadOnly( )                                                               Sets the invoking file to readonly.   

Also, because- File now supports the Comparable interface, the method compare To ( ) is also supported.

  • Feel free to send us an inquiry, we will reply back in hours.


Posted on September 17, 2014 in Input/output: Exploring

Share the Story

About the Author

Back to Top