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


Write a Java program that reads a file name from the user, then displays  information about whether the file exists, readable, writable, type of file and the length of the file in bytes
 
Week 4
 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.
Source Code:
 
import java.io.*;
class filedemo
{
           public static void p(String str)
           {
                  System.out.println(str);
           }
           public static void analyze(String s)
           {
                  File f=new File(s);
                  if(f.exists())
                  {
                  p(f.getName()+” is a file”);
                  p(f.canRead()?” is readable”:” is not readable”);
                  p(f.canWrite()?” is writable”:” is not writable”);
                  p(“Filesize:”+f.length()+” bytes”);
                  p(“File last mdified:”+f.lastModified());
                  }
                  if(f.isDirectory())
                  {
                  p(f.getName()+” is directory”);
                   p(“List of files”);
                   String dir[]=f.list();
                  for(int i=0;i<dir.length;i++)
                  p(dir[i]);
                  }
           }
          
}
public class FileDetails
{
           public static void main(String rr[])throws IOException
           {
                  filedemo fd=new filedemo();
                  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                  System.out.println(“Enter the file name:”);
                  String s=br.readLine();
                  fd.analyze(s);
           }
}
 
Expected Output:
 
 
D:java>java FileDetails
Enter the file name:
AA.java
AA.java is a file
 is readable
 is writable
Filesize:95 bytes
 
File last mdified:1549353789940
write a java program that reads a file name from the user then display

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

https://en.wikipedia.org/wiki/Filename

Have any Question or Comment?

Leave a Reply

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

Categories