Objective: 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.
Code:
import java.io.File;
import java.util.Scanner;
public class FileInformation
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the file name: ");
String fileName = scanner.nextLine();
File file = new File(fileName);
if (file.exists())
{
System.out.println("File exists: Yes");
System.out.println("Readable: " + (file.canRead() ? "Yes" : "No"));
System.out.println("Writable: " + (file.canWrite() ? "Yes" : "No"));
System.out.println("File Type: " + (file.isDirectory() ? "Directory" : "File"));
System.out.println("File Size: " + file.length() + " bytes");
} else {
System.out.println("File does not exist.");
}
scanner.close();
}
}
Output:
No comments:
Post a Comment