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 Code in JAVA:
import java.io.*;
class FileEx
{
public static void main(String[] args)
{
DataInputStream dis=new DataInputStream(System.in);
File f=new File("D:/Lab/abc/abc.txt");
System.out.println("File Name = "+f.getName());
System.out.println("File Path = " +f.getPath());
System.out.println("File parent = "+f.getParent());
System.out.println(f.isFile()?" is a File":"not a file");
System.out.println(f.isDirectory()?"it is a directory":"not a directory");
System.out.println(f.canRead()?" it is readable":"it is not readable");
System.out.println(f.canWrite()?"it is writable":"it is not writable");
System.out.println("length of file is"+f.length());
}
}
Program Output:
c:\java>javac FileEx.java
c:\java>java FileEx
File Name = abc.txt
File Path = D:\Lab\abc\abc.txt
File parent = D:\Lab\abc
is a File
not a directory
it is readable
it is not writable
length of file is 68
Post A Comment:
0 comments: