Google+

Like Us On Facebook


Total Pageviews

263. Methods of File Class







Till now, we have covered the following methods of File Class -
  • createNewFile( )
  • exists( )
  • getPath( )
  • getAbsolutePath( ) 
  • mkdir( )
Now lets cover the following methods of File Class in this post -
  1. canRead( )
  2. canWrite( )
  3. canExecute( )
  4. compareTo( )
  5. getParent( )
  6. isAbsolute( )
  7. isDirectory( )
  8. isFile( )
  9. delete( )
canRead( ), canWrite( ) and canExecute( ) methods -

These three methods will return true if the file permissions are set to read, write and execute. 

Lets Implement these methods on Eclipse IDE -

1. Launch Eclipse IDE, create a new Java Project 'FileClassMethods.java' with main( ) method in the existing Java Project as shown below -


2. Go the Project Workspace location, right click on the existing file 'fileX.txt' and select properties option as shown below -



3. In the Properties dialog, select 'Security' tab and view the file Permissions to read, write and execute as shown below -



As the file permissions are set to read, write and execute, so the methods canRead( ), canWrite( ) and canExecute( ) need to return true. Lets implement them in the below steps.

4. In Eclipse IDE, create a File Class object representing the 'fileX.java' file in our project workspace as shown below (Resolve import errors) -


5. Now implement the canRead( ), canWrite( ) and canExecute( ) methods as shown below -


6. Save & Run the Java Class 'FileClassMethods.java' and observe that the following statements got printed informing that the fileX.txt file is readable, writable and executable as shown below -


Hence we have used canRead( ), canWrite( ) and canExecute( ) methods to find the read, write and execute permissions of the file.

compareTo( ) method -

This method is used to compare the specified file paths. If the paths are equal, the method returns 0 else it will return some number. We have to implement the compareTo( ) method as specified in the below example -

Example -

File file2 = new File("ABZ//fileY.txt");
File file3 = new File("ABZ//fileY.txt");

if( file2.compareTo(file3) == 0)
   System.out.println("Paths of file2 and file3 are same");
else
   System.out.println("Paths of file2 and file3 are different");

Lets implement this on Eclipse IDE -

1. In Eclipse IDE, in FileClassMethods.java class file, create two more File Class objects as shown below -


2. Compare the paths of two File Class objects file2 and file3 as shown below -


 3. Save & Run the Java Class 'FileClassMethods.java' and observe that paths are same is displayed in the output as shown below -


Hence we have used compareTo( ) method to compare the paths of the File Class objects.

getParent( ) method -

This parents gets the Parent folder of the file as shown in the below example -

File file2 = new File("ABZ//fileY.txt");   //ABZ is the parent folder of fileY.txt file

System.out.println("Parent folder of fileY.txt file is "+ file2.getParent( ) );  //Prints the ABZ folder

Lets implement this on Eclipse IDE -

1. In Eclipse, in 'FileClassMethods.java' Class, write the following statement to find and print the Parent folder of the File Class object file2 as shown below -


2. Save & Run the Java Class 'FileClassMethods.java' and observe that the Parent Folder of the fileY.txt file is printed in the output as shown below -


Hence we have used getParent( ) method to find the parent folder of the fileY.txt file.

isAbsolute( ) method -

This methods help us in finding whether the specified path in the File Class object creation statement is absolute or not. The method returns true when the path specified in File Class object creation statement is absolute path (i.e. complete path), else the methods will return false as shown in the below example -

Example -

File file1 = new File("fileX.txt");  //Relative path

System.out.println("file1 object path is Absolute - "+ file1.isAbsolute( ) );   //Output will be false

Lets implement this on Eclipse IDE -

1. In Eclipse, in 'FileClassMethods.java', implement isAbsolute( ) method as shown below -


2. Save & Run the Java Class 'FileClassMethods.java' and observe that the path of the file1 is not an absolute path is printed by printing false as shown below -



Hence we have used isAbsolute( ) method to find out whether the path of a file is absolute or relative.

isFile( ) method -

This methods returns true if it is a file, else it returns false as shown in the below example -

File file1 = new File("fileX.txt");   //file

System.out.println("file1 is a file - " + file1.isFile( ) );   //Returns true

Lets implement this on Eclipse IDE -

1. In Eclipse IDE, in 'FileClassMethods.java' Class, implement the isFile( ) to find whether file1 object is holding a file or not as shown in the below example -


2. Save & Run the 'FileClassMethods.java' and observe that  filel is holding a file is printed by printing true in the output as shown below -


Hence we have used isFile( ) method to find out whether the File Class object is holding a file or not.

isDirectory( ) method -

This method is used to find out whether the File Class object is holding a folder or not as shown in the below example -

File dir1 = new File("ABZ");  //Folder

System.out.println("dir1 is holding a folder - " + dir1.isDirectory( ) );  //Results true

Lets implement this on Eclipse IDE -

1. In Eclipse IDE, in 'FileClassMethods.java' Class, create a File Class object representing a folder as shown below -


2. Implement the isDirectory( ) method to find out whether dir1 is holding a folder or not as shown below -


3. Save & Run the 'FileClassMethods.java' file and observe that File Class object dir is holding a folder is printed as true in output as shown below -



Hence we have used isDirectory( ) method to find out whether the File Class object is holding a folder or not.

length( ) method -

This method will print the number of characters in a file as shown in the below example -

File file1 = new File("fileX.txt"); //I have written "Hi" in this text file
System.out.println("length of the text file fileX.txt is "+file1.length( )  );  //2 will be printed

Lets implement this on Eclipse IDE -

1. Go to the Project Workspace path and find any text file say 'fileX.txt' as shown below -



2. Open the text file 'fileX.txt' from the Project Workspace, enter 'Hi' text into it and save it as shown below -



3. In Eclipse IDE, in 'FileClassMethods.java' Class, implement the length( ) method to find out the length of the fileX.txt file in project workspace as shown below -


4. Save & Run the 'FileClassMethods.java' and observe that length is printed as 2 as shown below -


Hence we have used length( ) method to get the length of text in files.

delete( ) method -

This method is used to delete files or folders and returns true on deletion as shown in the below example -

File file1 = new File("fileX.txt"); //file
File dir1 = new File("ABZ"); //folder

System.out.println("fileX.txt is deleted - " + file1.delete( ) );   //prints true
System.out.println("ABZ folder is deleted - "+dir1.delete( ) );  //prints true

Lets implement this on Eclipse IDE -

1. Go to project workspace and find the file 'fileX.txt' and folder 'ABZ' are available as shown below -



2. In Eclipse IDE, in 'FileClassMethods.java' class, implement the delete( ) method to delete the 'fileX.txt' file and 'ABZ' folder as shown below -



3. Save & Run the Java Class 'FileClassMethods.java' and observe that the file and folder deletion is printed as true in the output as shown below -


4. Go the Project Workspace and find that the file 'fileX.txt' and folder 'ABZ' got deleted as shown below -


Hence we have used delete( ) method to delete the file and folder.

With this we have covered the methods of File Class.






Please comment below to feedback or ask questions.

I/O Streams will be explained in the next post.










No comments:

Blog Index