Delete a file

From Rosetta Code
Revision as of 14:58, 7 April 2007 by 70.83.182.253 (talk) (New page: {{task}} In this task, the job is to delete a file called "input.txt" and delete a directory called "docs". Assuming current directory or fullpath. Either "/input.txt" or "\input.txt" for...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Task
Delete a file
You are encouraged to solve this task according to the task description, using any language you may know.

In this task, the job is to delete a file called "input.txt" and delete a directory called "docs". Assuming current directory or fullpath. Either "/input.txt" or "\input.txt" for the former and "/docs/" or "\docs\" for the second test.


Java

public class FileDeleteTest {
   public static boolean deleteFile(String filename) {
       boolean exists = new File(filename).delete();
       return exists;
   }
   public static void test(String type, String filename) {
       System.out.println("The following " + type + " called " + filename + 
           (deleteFile(filename) ? " was deleted." : " could not be deleted.")
       );
   }
   public static void main(String args[]) {
        test("file", "input.txt");
        test("file", File.seperator + "input.txt");
        test("directory", "docs");
        test("directory", File.seperator + "docs" + File.seperator);
   }
}