Check that file exists: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 30: Line 30:
#!/usr/bin/perl
#!/usr/bin/perl
sub isFileExists($) {
sub isFileExists($) {
return -e $_[0];
my ($filename) = @_;
return -e $filename;
}
}
sub test($$) {
sub test($$) {
my ($type, $filename) = @_;
print "The following " . $_[0] . " called " . $_[1] .
(isFileExists($_[1]) ? " exists." : " not exists.");
print "The following " . $type . " called " . $filename .
(isFileExists($filename) ? " exists." : " not exists.");
}
}
my $FileSeperator = ($^O eq "MSWin32") ? "\\" : "/";
test("file", "input.txt");
test("file", "input.txt");
test("file", "/input.txt");
test("file", $FileSeperator . "input.txt");
test("file", "\\input.txt");
test("directory", "docs");
test("directory", "docs");
test("directory", "/docs/");
test("directory", $FileSeperator ."docs". $FileSeperator);
test("directory", "\\docs\\");
exit;
exit;
# Short version
# Short version
my $FileSeperator = ($^O eq "MSWin32") ? "\\" : "/";
print -e 'input.txt';
print -e 'input.txt';
print -e '/input.txt';
print -e $FileSeperator.'input.txt';
print -e "\\input.txt";
print -e 'docs';
print -e 'docs';
print -e '/docs/';
print -e $FileSeperator.'docs'.$FileSeperator;
print -e "\\docs\\";

Revision as of 15:51, 7 April 2007

Task
Check that file exists
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 verify that a file called "input.txt" exists or not and that the directory called "docs" exists or not. Assuming current directory or fullpath. Either "/input.txt" or "\input.txt" for the former and "/docs/" or "\docs\" for the second test.


Java

import java.util.File;
public class FileExistsTest {
   public static boolean isFileExists(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 + 
           (isFileExists(filename) ? " exists." : " not exists.")
       );
   }
   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);
   }
}

Perl

  #!/usr/bin/perl
  sub isFileExists($) {
      my ($filename) = @_;
      return -e $filename;
  }
  sub test($$) {
      my ($type, $filename) = @_;
      print "The following " . $type . " called " . $filename . 
          (isFileExists($filename) ? " exists." : " not exists.");
  }
  my $FileSeperator = ($^O eq "MSWin32") ? "\\" : "/";
  test("file", "input.txt");
  test("file", $FileSeperator . "input.txt");
  test("directory", "docs");
  test("directory", $FileSeperator ."docs". $FileSeperator);
  exit;

  # Short version
  my $FileSeperator = ($^O eq "MSWin32") ? "\\" : "/";
  print -e 'input.txt';
  print -e $FileSeperator.'input.txt';
  print -e 'docs';
  print -e $FileSeperator.'docs'.$FileSeperator;