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

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

Aim: Introduce File operations in java.

Program:-

import java.io.File;
import java.util.Scanner;
class FileP
{
public static void main(String args[ ])
{
Scanner obj=new Scanner(System.in);
String fname=obj.next();
File f1 = new File(fname);
System.out.println("File Name: " + f1.getName());
f1.setWritable(false);
System.out.println(f1.exists() ? "File exists" : "File does not exist");
System.out.println(f1.canWrite() ? "File is writeable" : "File is not writeable");
System.out.println(f1.canRead() ? "File is readable" : "File is not readable");
String fileName = f1.toString();
int index = fileName.lastIndexOf('.');
if(index > 0)
{
}
else
{
}
String type = fileName.substring(index + 1);
System.out.println("File type is " + type);
System.out.println("File doesn't have type");
System.out.println("File size: " + f1.length() + " Bytes");
}
}

Output:-

Leave a Reply

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