Create a file: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎[[Java]]: Use Java header instead)
m (Changed over to headers.)
Line 5: Line 5:
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.
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.


==[[DOS Batch File]]==
=={{header|DOS Batch File}}==
[[Category:DOS Batch File]]
md docs
md docs
md \docs
md \docs


==[[Forth]]==
=={{header|Forth}}==
[[Category:Forth]]
There is no means to create directories in ANS Forth.
There is no means to create directories in ANS Forth.
s" output.txt" w/o create-file throw ( fileid) drop
s" output.txt" w/o create-file throw ( fileid) drop
Line 45: Line 43:
}
}


==[[MAXScript]]==
=={{header|MAXScript}}==
[[Category:MAXScript]]
-- Here
-- Here
f = createFile "output.txt"
f = createFile "output.txt"
Line 56: Line 53:
makeDir ("c:\docs")
makeDir ("c:\docs")


==[[Perl]]==
=={{header|Perl}}==
[[Category:Perl]]
use File::Spec::Functions qw(catfile rootdir);
use File::Spec::Functions qw(catfile rootdir);
{ # here
{ # here
Line 78: Line 74:
perl -e 'mkdir "/docs"'
perl -e 'mkdir "/docs"'


==[[Python]]==
=={{header|Python}}==
[[Category:Python]]


Current directory
Current directory
Line 93: Line 88:
os.mkdir("/docs")
os.mkdir("/docs")


==[[Raven]]==
=={{header|Raven}}==
[[Category:Raven]]


"" as str
"" as str
Line 102: Line 96:
'/docs' mkdir
'/docs' mkdir


=={{header|SmallTalk}}==
==[[Smalltalk]]==
[[Category:Smalltalk]]


"Smalltalk has no notion of 'current directory' because Smalltalk isn't tied to the shell that created it."
"Smalltalk has no notion of 'current directory' because Smalltalk isn't tied to the shell that created it."
Line 109: Line 102:
(FileDirectory on: 'c:\') newFileNamed: 'output.txt'; createDirectory: 'docs'.
(FileDirectory on: 'c:\') newFileNamed: 'output.txt'; createDirectory: 'docs'.


==[[Tcl]]==
=={{header|Tcl}}==
[[Category: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):
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):
Line 120: Line 112:
file mkdir [file nativename /docs]
file mkdir [file nativename /docs]


==[[Toka]]==
=={{header|Toka}}==
[[Category:Toka]]


needs shell
needs shell
Line 131: Line 122:
" /docs" &777 mkdir
" /docs" &777 mkdir


==[[Visual Basic .NET]]==
=={{header|Visual Basic .NET}}==
[[Category:Visual Basic .NET]]


'''Platform:''' [[.NET]]
'''Platform:''' [[.NET]]
Line 150: Line 140:
IO.File.Create(IO.Path.DirectorySeparatorChar & "output.txt").Close()
IO.File.Create(IO.Path.DirectorySeparatorChar & "output.txt").Close()


==[[UNIX Shell]]==
=={{header|UNIX Shell}}==
[[Category:UNIX Shell]]


touch output.txt
touch output.txt

Revision as of 20:14, 12 November 2007

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" 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.

DOS Batch File

 md docs
 md \docs

Forth

There is no means to create directories in ANS Forth.

 s" output.txt" w/o create-file throw ( fileid) drop
s" /output.txt" w/o create-file throw ( fileid) drop

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);
   }
}

MAXScript

-- Here
f = createFile "output.txt"
close f
makeDir (sysInfo.currentDir + "\docs")
-- System root
f = createFile "\output.txt"
close f
makeDir ("c:\docs")

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

import os
f = open("output.txt", "w")
f.close()
os.mkdir("docs")

Root directory

os.mkdir("docs")
f = open("/output.txt", "w")
f.close()
os.mkdir("/docs")

Raven

"" as str
str 'output.txt'  write
str '/output.txt' write
'docs'  mkdir
'/docs' mkdir

SmallTalk

"Smalltalk has no notion of 'current directory' because Smalltalk isn't tied to the shell that created it."
 (FileDirectory on: 'c:\') newFileNamed: 'output.txt'; createDirectory: '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]

Toka

 needs shell
 " output.txt" "W" file.open file.close
 " /output.txt" "W" file.open file.close

 ( Create the directories with permissions set to 777)
 " docs" &777 mkdir
 " /docs" &777 mkdir

Visual Basic .NET

Platform: .NET

Language Version: 9.0+

'Current Directory
IO.Directory.CreateDirectory("docs")
IO.File.Create("output.txt").Close()

 'Root
IO.Directory.CreateDirectory("\docs")
IO.File.Create("\output.txt").Close()

 'Root, platform independent
IO.Directory.CreateDirectory(IO.Path.DirectorySeparatorChar & "docs")
IO.File.Create(IO.Path.DirectorySeparatorChar & "output.txt").Close()

UNIX Shell

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