The  program should accept command line argument,  if  it file then File name display and if it Directory then contains of directory  is displays.

 

import java.io.*;

public class FileDemo {

 

   public static void main(String as[]) 

 { 

      File f=null;

      try {

f=new File(as[0]);

System.out.println(as[0]);

if(f.isDirectory())

{

String[] ls=f.list();

for(int i=0;i<ls.length;i++)

System.out.println(ls[i]);

}

else

System.out.println(f.getPath());

         }

 

catch(Exception e2){

System.out.println(e2);}

finally {

         System.out.println("now program ends....");

         }

              

 

   }

}


1.Output:


E:\sysem3>java FileDemo e:\sysem3\finaldemo.java

E:\sysem3\finaldemo.java

E:\sysem3\finaldemo.java

now program ends....

 

2.Output:


E:\sysem3>java FileDemo e:\sysem3

e:\sysem3

A.class

AbsClass.class

AnonymousClass$1.class

AnonymousClass.class

AnonymousClass.java

AnonymousClassDemo$1.class

AnonymousClassDemo.class

AnonymousClassDemo.java

AnonymousInner.class

arraylist.class

arraylist.java

B.class

basic.class

basic.java

basic1.class

basic1.java

C.class

calc.class

calc.java

cmdbasic.class

cmdbasic.java

cmdnames.class

cmdnames.java

CopyFile.class

CopyFile.java

D.class

datedemo.class

datedemo.java

diffwindows.class

…..

Now program ends