Tuesday, 8 April 2014

Directory operations using File class in java


/*
Directory operation 
Listing all the contents of a directory if it exists
*/

import java.io.*;

class Main
{
       public static void main(String []args)
       {
              String dirName=".";
              if(args.length==1)
                    dirName=args[0];
                   
              File d=new File(dirName);
             
              if(!d.exists())
              {
                    System.err.println("Directory Not Found - "+dirName);
                    System.exit(1);
              }
              if(!d.isDirectory())
              {
                    System.err.println("No such directory exists, "+dirName+" is a File");
                    System.exit(1);
              }
                   
              File arr[]=d.listFiles();
              for(File f:arr)
                    System.out.println(f.getName());
       }
}


No comments:

Post a Comment