Create a file
Create a file
You are encouraged to solve this task according to the task description, using any language you may know.
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" of size 0 byte and an empty directory called "docs". This should be done twice: once "here", i.e. in the current working directory and once in the filesystem root.
Bash
touch output.txt touch /output.txt mkdir docs mkdir /docs
DOS
md docs md \docs
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
use File::Spec::Functions qw(catfile rootdir); { # here open my $fh, '>', 'output.txt'; mkdir 'docs'; }; { # root dir open my $fh, '>', catfile rootdir, 'output.txt'; mkdir catfile rootdir, 'docs'; };
Without Perl Modules
Current directory
perl -e 'qx(touch output.txt)' perl -e 'mkdir docs'
Root directory
perl -e 'qx(touch /output.txt)' perl -e 'mkdir "/docs"'
Python
Current directory
f = open("output.txt", "w") f.close()
Root directory
f = open("/output.txt", "w") f.close()
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]
Toka
needs shell " output.txt" "W" file.open file.close " /output.txt" "W" file.open file.close
( 511 is the decimal equivilient of mode 777) " docs" 511 mkdir " /docs" 511 mkdir