Create a file

From Rosetta Code
Task
Create 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 create a new empty file called "output.txt" with no content and of size 0 byte and an empty directory called "docs". Assuming current directory or fullpath. Either "/output.txt" or "\output.txt" for the former and "/docs/" or "\docs\" for the second test.

Java

import java.util.File;
public class CreateFileTest {
   public static String createNewFile(String filename) {
       try {
           // Create file if it does not exist
           boolean success = new File(filename).createNewFile();
           if (success) {
               return " did not exist and was created successfully.";
           } else {
               return " already exists.";
           }
       } catch (IOException e) {
               return " could not be created.";
       }
   }
   public static void test(String type, String filename) {
       System.out.println("The following " + type + " called " + filename + 
           createNewFile(filename)
       );
   }
   public static void main(String args[]) {
        test("file", "output.txt");
        test("file", File.seperator + "output.txt");
        test("directory", "docs");
        test("directory", File.seperator + "docs" + File.seperator);
   }
}

Perl

  sub createNewFile($) {
      my ($filename) = @_;
      if(-e $filename) { return " already exists."; }
      open(FH, ">> ". $filename) or return " could not be created.";
      close(FH);
      return " did not exist and was created successfully.";
  }
  sub createNewDir($) {
      my ($filename) = @_;
      if(-e $filename) { return " already exists."; }
      mkdir($filename) or return " could not be created.";
      return " did not exist and was created successfully.";
  } 
  sub test($$) {
      my ($type, $filename) = @_;
      print "The following " . $type . " called " . $filename . 
          ($type eq "file") ? createNewFile($filename) : createNewDir($filename);
  }
  my $FileSeperator = ($^O eq "MSWin32") ? "\\" : "/";
  test("file", "output.txt");
  test("file", $FileSeperator . "output.txt");
  test("directory", "docs");
  test("directory", $FileSeperator . "docs" . $FileSeperator);
  exit;
  # Short version;
  my $FileSeperator = ($^O eq "MSWin32") ? "\\" : "/";
  open(FH, ">> output.txt") and close(FH);
  open(FH, ">> ".$FileSeperator."output.txt") and close(FH);
  mkdir("docs");
  mkdir($FileSeperator."docs".$FileSeperator);

Bash

 touch output.txt
 touch /output.txt
 mkdir docs
 mkdir /docs

DOS

 md docs
 md \docs

Tcl

Assuming that we're supposed to create two files and two directories (one each here and one each in the file system root) and further assuming that the code is supposed to be portable, i.e. work on win, linux, MacOS (the task is really not clear):

close [open output.txt w] 
close [open [file nativename /output.txt] w] 

file mkdir docs
file mkdir [file nativename /docs]