(Using java.lang.FileInputStream and FileOutputStream) a) Write a Java program that reads a file name from the user, then displays information about whether the file exists, whether the file is readable, whether the file is writable, the type of file and the length of the file in bytes.

//Program to read a file and display its properties
import java.io.*;
import java.util.*;
import javax.swing.*;
class Fileproperties
{
            public static void main(String args[])throws IOException
            {
                        Scanner scr=new Scanner(System.in);
                        System.out.print(“nEnter any file name: “);
                        String str=scr.nextLine();
                        File f=new File(str);
                        JFileChooser c=new JFileChooser();
                        if(f.exists())
                        {
                                    System.out.println(f.canRead()?”File is Readable”:”File is not Readable”);
                                    System.out.println(f.canWrite()?”File is Writable”:”File is not Writaable”);
                                    System.out.println(“Length of the file is : “+f.length());
                                    System.out.println(“nType of the file is: “+c.getTypeDescription(f));
                        }
                        else
                        System.out.println(“File does not exists”);
            }
}
Output:
Enter any file name: Fileproperties.java
File is Readable
File is Writable
Length of the file is : 778

Type of the file is: JAVA File

Leave a Reply

Your email address will not be published. Required fields are marked *

Enable Notifications OK No thanks