Create a file

From CodeCodex

Implementations[edit]

Erlang[edit]

create_file(Filename) ->
    case file:open(Filename, [write]) of
        {ok, IoDevice} ->
            file:close(IoDevice);
        {error, Reason} ->
            io:format("~s open error  reason:~s~n", [Filename, Reason]),
            ng
    end.

Java[edit]

    try {
        File file = new File("filename");
    
        // Create file if it does not exist
        boolean success = file.createNewFile();
        if (success) {
            // File did not exist and was created
        } else {
            // File already exists
        }
    } catch (IOException e) {
    }

Python[edit]

import os
os.mknod("filename")

The mknod(2) manpage suggests this might not be portable, so:

file("filename", "w").close()

Ruby[edit]

open("filename", "w").close

Tcl[edit]

close [set f [open filename.txt w]]