All About Class File in Java




An instance of this class File represents a file or a directory on the host file system.A file is specified by a path name which can either be an absolute path name or a path name relative to current working directory.The path name must follow the naming convention of the host platform.The File class is intended to provide an abstraction that deals with most of the known machines dependent complexities of files and path names in a machine independent manner.

The class File  is structured as:

public class java.io.File extends java.lang.Object{
//Member elements:
public final static String pathSeparator;
//the system dependent path separator String.This field is initialized to contain the value of System
// property.
public final static char pathSeparatorChar;
//the system dependent path separator Char.This filed is initialized to contain the first character 
//value of the system property.This character is often used to separate file names in a sequence of 
//files given as a path list.
public final static String separator;
//the system dependent path separator String.This field is initialized to contain the value of 
//System property.
public final static char separatorChar;
//the system dependent path separator character.This filed is initialized to contain the first character 
//value of the system property.This character separates the directory and file components in a filename.
//constructors:
public File(File dir,String name);
//creates a file instance that represents the file with the specified name in the specified directory.If
//the directory argument is null,the resulting file instance represents a File in the system dependent
// current directory whose path name is the pathname of the directory,followed by the separator 
//character,followed by name argument.
public File(String path);
//creates a file instance that represents the file whose pathname is the given argument.It may throw 
//NullPointerException if the file path is equal to null.
public File(String path,String name);
//creates a file instance that represents the file whose pathname is the pathname of the specified directory 
//followed by the separator character followed by the name argument.
//Methods:
public boolean canRead();//returns true if the file readable,false otherwise
public boolean canWrite();//returns true if the file is writable,false otherwise
public boolean delete();//returns true if the file is successfully deleted.This only operates on simple file object
//It can not delete an empty directory.
public boolean equals(Object obj);
public boolean exists();//returns true if the file exists, false otherwise
public boolean isAbsolute();//returns true if the file is with in the system false if it is relative.
public boolean isDirectory();//returns true if directory is a directory object,false otherwise
public boolean isFile();//returns true if the object is a file,false otherwise
public boolean mkdir();//this method creates the directory,returns true if successful.False indicate that 
//the path specified in the file object already exists or the directory can not be created because the path 
//does not exist.
public boolean mkdirs();//this will return true after creating where path of the directory may not exist.It 
//not only creates a directory but also will create the parents of the directory.
public boolean renameTo(File destFileName);//returns true if the operation is successful and false if one 
//of the cases happens-1. File does not exists 2. File name is the same as renamed one.
public String getAbsolutePath();//returns the absolute path of a file when it is not in local
public String getName();//returns the name of the file object
public String getParent();//returns the parent directory in which the file object exists
public String getPath();//returns the path of the file object
public String[] list();//returns the list of all files in a directory.
public String[] list(FilenameFiler filter);
public long lastModified();//returns the date of the last modification in long format
public long length();//returns the length of the file
public int hashCode();
public String toString();
 
An Example

public class MyFileTest{

     public static void main(String []args) throws IOException{
        File fileToCheck;
        if(args.length>0)
        {
            for(int i=0;i<args.length;i++)
            {
                fileToCheck=new File(args[i]);
                getNames(fileToCheck);
                getInfo(fileToCheck);
            }
        }
        else{
            System.out.println("File error")
        }
     }
     public static void getName(File f) throws IOException{
         System.out.println("Name"+f.getName());
         System.out.println("Path"+f.getPath());
         System.out.println("Parent"+f.getParent());
     }
     public static void getInfo(File f) throws IOException{
         if(f.exists)
         {
             System.out.println("File Exists");
         }
         else
         {
             System.out.println("File does not Exists");
         }
         if(f.canRead())
         {
             System.out.println("File is readable");
             System.out.println("File last modified by"+f.lastModified());
             System.out.println("File is"+f.length()+"bytes");
         }
         else{
             System.out.println("File is not readable");
         }
         if(f.canWrite())
         {
             System.out.println("File is writeable");
         }
         else
         {
             System.out.println("File is not writable")
         }
         }
     }
}
 
File class does not allow us to read or write to a file.To read or write we have other classes like

All About Class File in Java All About Class File in Java Reviewed by Animesh Chatterjee on October 09, 2018 Rating: 5

No comments:

Powered by Blogger.