Files Assignment Help

Assignment Help: >> IO Stream - Files

Files

Therefore most of the classes declared through java.io operate on streams, the File class does not.  That deals directly along 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.  File objects is used to acquire or manipulate the information related with a disk file, like as the permissions, date, time, and directory path, and to navigate subdirectory hierarchies.

Files are a primary source and destination for data inside several programs. While there are severe restrictions on their use inside applets for security reasons, files are still a central resource for storing persistent and shared data. A directory in Java is treated simply as a File along with one additional property - a list of filenames in which a can be examined through the list ( ) method

The given constructors could be used to create File objects

File( String directory Path)

File( String directoryPath, String filename)

File(FiledirObj,String filename)

Above directoryPath is the path name of the file, filename is the name of the file, and dirObj is a File object which specifies a directory.

The given example creates three files that are: f1, f2 and f3. The first File object is constructed along with a directory path as the only argument. The second involves two arguments - the path and the filename. The third involves the file path assigned to f1 and a filename f3 refers to the same file as f2.

File f1 = new File ("/");

File f2 = new File ("/" , "autoexec.bat");

File f3 = new File (f1,"autoexc.bat");

A File declares several methods that acquire the standard properties of a File object. For example, getParent ( ) returns the name of the parent directory, getName ( ) returns the name of the file and exists ( ) returns true if the file exists, false if it does not. A File class, by, is not symmetrical.  By this, we mean in which there are several methods which permit you to examine the properties of an easy file object, but no corresponding function exists to change those attributes. The given example demonstrates various of the File methods.

/ / Demonstrate File.

import java.io.File;

Class FileDemo {

Static void p (String s)         {

System.out.println (s);

}

public static void main (String args [ ] ) {

File f1 - new File ("/java/COPYRIGHT");

p("File Name: " + f1.getName ( ) );

p("Path: "+ f1.getPath ( ) );

p ("Abs:" + f1 getAbsolutePath ( ) );

p ("Parent: " + f1.getAbsolutePath ( ) );

p(f1.exists ( ) ? "exists" : does not exist" );

p (f1.canWrite ( ) ? "is writeable" : is not writeable");

p (f1.canRead ( ) ? "is readable : "is not readable");

p ("is" + (f1.isDirectory ( ) ? " " : "not" + "a directory");

p (f1.isFile ( ) ? "is normal file" : "might be a named pipe");

p (f1.isAbsoulte ( ) ? "is absolute" : "is not abosulte" );

p ("File last modified:" + f1.lastModified ( ));

p ("File size: " + f1 length ( ) + "Bytes" );

Whenever you run this program, you will see something same to the given output:

File name : COPYRIGHT

Path :/ java/COPYRIGHT

Abs :/java/COPYRIGHT

Parent: /java exixts

is writeable is readable

is not a directory

is normal file

is absolute

File last modified : 812465204000

File size : 695 Bytes

Most of the File functions are self explanatory. Absoulte ( ) and isFile ( ) is not. An isFile ( ) return true if called on a file and false if called on a directory. In addition, isFile ( ) returns false for a few special files, like as device drivers and named pipes, so this method can be used to make sure the file will behave as a file. An isAbsoulte ( ) method returns true if the file has an absoulte path and false if its path is associative.

File also involves two useful utility methods. The first is rename To ( ), display here:

boolean renameTo (File newName)

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

The second utility method is delete ( ) that deletes the disk file represented through the path of the invoking File object. It is displays here:

boolean delete( )

You can also use delete ( ) function to delete a directory if the directory is empty.  delete ( ) returns if it deletes the file and false if the file cannot be erased.

Java  2  adds  a few  new  method  to  File  which  you  may  search  helpful  in  certain conditions. A few of the most interesting are should here:

Methods                                                                    Description

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

boolean isHidden ( )                      Returns  true  if  the  invoking  file  is  hidden. Return false Otherwise.

boolean setLastModified                 Sets the time stamp on the invoking file to that

(long Millisec)                        specified by millisec, which is the number of milliseconds from January 1, 1970, Coordinated Universal Time (UTC).

Boolean setReadOnly ( )                Sets the invoking file to read only.

Also, since File now supports the Comparable interface, a methods compareTo( )is Also supported.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd