File Handling - File Properties

// File Handling
// File Properties

/*
Write a Java program that reads a file name from the user, displays information about whether the file exists, whether the file is readable, or writable, the type of file and the length of the file in bytes.
*/

import java.io.*;

class FileHandling
{
     public static void main(String as[]) throws IOException
     {
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
          System.out.print("Enter string : ");
          String str = br.readLine().trim();
         
          File f1 = new File(str);
         
          if (f1.isFile()==true)
          {
             
              System.out.println("File exists = " + f1.exists());
             
              System.out.println("File name = " + f1.getName());
              System.out.println("Parent File = " + f1.getParent());
             
              System.out.println("File path = " + f1.getPath());
              System.out.println("If absolute path of file is specified = "+f1.isAbsolute());
              if(f1.isAbsolute()==true)
                   System.out.println("File absolute path = "+f1.getAbsolutePath());
             
              System.out.println("File type = " + str.substring(str.lastIndexOf('.')));
    
    
              System.out.println("File readable = " + f1.canRead());
              System.out.println("File writable = " + f1.canWrite());      
             
              System.out.println("File last modified = " + f1.lastModified());
                  
              System.out.println("File length = " + f1.length() + " bytes");

              }       
          else
              System.out.println("Not a file");
     }
}



/*

Sample output:

     D:\oopjava\iostreams>javac FileHandling.java
     D:\oopjava\iostreams>java FileHandling

     Enter string : FileHandling.java
     File exists = true
     File name = FileHandling.java
     Parent File = null
     File path = FileHandling.java
     If absolute path of file is specified = false
     File type = .java
     File readable = true
     File writable = true
     File last modified = 1537860249825
     File length = 1396 bytes

     D:\oopjava\iostreams>


*/

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu