Check that file exists

Revision as of 14:30, 7 April 2007 by 70.83.182.253 (talk) (New page: {{task}} In this task, the job is to verify that a file called "input.txt" exists or not and that the directory "docs" exists or not. Assuming current directory or fullpath. Either "/inpu...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

Task
Check that file exists
You are encouraged to solve this task according to the task description, using any language you may know.


Java

public class FileTest {
   public static boolean isFileExist(String filename) {
       boolean exists = new File(filename).exists();
       return exists;
   }
   public static void test(String type, String filename) {
       System.out.println("The following " + type + " called " + filename + 
           (isFileExist(filename) ? " exists." : " not exists.")
       );
   }
   public static void main(String args[]) {
        test("file", "input.txt");
        if(File.seperatorChar == '/')
            test("file", "/input.txt");
        else
            test("file", "c:\\input.txt");
        test("directory", "docs");
        if(File.seperatorChar == '/')
            test("directory", "/docs/");
        else
            test("directory", "c:\\docs\\");
   }
}