Source of Question11.java


  1: import java.io.File;
  2: import java.util.Scanner;
  3: public class Question11
  4: {
  5:     public static void main(String[] args)
  6:     {
  7:         System.out.print("Enter a file name and I will ");
  8:         System.out.println("tell you whether it exists."); 
  9:         Scanner keyboard = new Scanner(System.in);
 10:         String name = keyboard.next( ); 
 11:         File fileObject = new File(name);
 12:                 
 13:         if (fileObject.exists( )) 
 14:         {
 15:             System.out.println("I found the file " + name);
 16:             System.out.println("Delete the file?");
 17:             String ans = keyboard.next( );
 18:             if (ans.equalsIgnoreCase("yes"))
 19:             {
 20:                 System.out.println("If you delete the file " +
 21:                                     name);
 22:                 System.out.println("all data in the file " +
 23:                                    "will be lost.");
 24:                 System.out.println("Delete?");
 25:                 ans = keyboard.next( );
 26:                 if (ans.equalsIgnoreCase("yes"))
 27:                 {
 28:                     if (fileObject.delete( ))
 29:                         System.out.println("File deleted.");
 30:                     else
 31:                          System.out.println("CanÕt delete.");
 32:                 }
 33:                 else
 34:                     System.out.println("File not deleted.");
 35:             }
 36:             else
 37:                 System.out.println("File not deleted.");
 38:         }
 39:         else
 40:             System.out.println("I cannot find " + name);
 41:     }
 42: }