Accessing the file system


This tutorial demonstrates some of the filesystem operations available to haxe programs. The sys.FileSystem class contains most of the methods we will use.

Work in progress documentation for future Haxe 3.0.
// TODO: need to check what the neko.Lib.println will be, so only partially updated based on neko one still.

Pre/Post Conditions

The example uses an existing text file. Before running the program, create a file in the current directory. It can contain anything or can be empty. If it's not named "input.txt," pass its name to the program on the command line.

When the program completes there will be a subdirectory in the current directory named "subdir" which contains a copy of the input file. The filename of the copy will be the name of the input file with "renamed_" prepended to it.


In this example, we will:
- check that the input file exists
- query the system for the file's access time, size, etc.
- check if a directory exists.
- create a subdirectory
- make two copies of the input file in the subdirectory
- rename one of the copies
- make a directory listing of the subdirectory
- delete one of the copies in the subdirectory


At each step, we check to see if input files exist before they are read. We also check if output files exist after they've been written. If a file is expected and not found, the program reports the error and exits.

The following statements both return true if dirname is the name of an existing directory:
- sys.FileSystem.isDirectory(dirname)
- sys.FileSystem.kind(dirname) == sys.FileKind.kdir

There is no "path.join" method. Instead, just make a string using either / or \ as the directory separator.

Running the program a second time will not give the same output since the subdirectory and renamed file will already exist. Repeated runs will satisfy the post-conditions stated above, though.

The Code

import sys.FileSystem;
import sys.Lib;

class FileSystemExample
  static function main()
    // get filename from command line arg, or use "input.txt" if no arg specified
    var fname = ( sys.Lib.args().length==0) ? "input.txt" : sys.Lib.args()[0];

    // check that file exists
    if( sys.FileSystem.exists(fname) )
    #if neko
      neko.Lib.println("file exists");
   #else if php
      php.Lib.println('file exists');
   #else if cpp
    cpp.Lib.printLn('file exists');
    #if neko
      neko.Lib.println("file not found");
   #else if php
      php.Lib.println("file not found");
   #else if cpp
    cpp.Lib.printLn("file not found");
      sys.Lib.exit(1); // quit

    // get absolute path
    #if neko
      neko.Lib.println("filename: " + sys.FileSystem.fullPath(fname));

   #else if php
      php.Lib.println("filename: " + sys.FileSystem.fullPath(fname));

   #else if cpp
    cpp.Lib.printLn("filename: " + sys.FileSystem.fullPath(fname));


    // check file attributes
    var stat:FileStat = sys.FileSystem.stat(fname);
    #if neko
        neko.Lib.println("Last access time: " + stat.atime);
        neko.Lib.println("Last modification time: " + stat.mtime);
       neko.Lib.println("Last status change time: " + stat.ctime);
   #else if php
        php.Lib.println("Last access time: " + stat.atime);
        php.Lib.println("Last modification time: " + stat.mtime);
        php.Lib.println("Last status change time: " + stat.ctime);
   #else if cpp
    cpp.Lib.println("Last access time: " + stat.atime);
        cpp.Lib.println("Last modification time: " + stat.mtime);
        cpp.Lib.println("Last status change time: " + stat.ctime);

    // create a subdirectory if it doesn't already exist
    var dirname = "subdir";
    if( !sys.FileSystem.exists(dirname) )

      // check that it was successfull
      if( sys.FileSystem.exists(dirname) && sys.FileSystem.isDirectory(dirname)
      && sys.FileSystem.kind(dirname) == sys.FileKind.kdir )

    neko.Lib.println("subdirectory " + dirname + " was created");

    neko.Lib.println("subdirectory creation failed");
    neko.Sys.exit(1); // quit
    neko.Lib.println("subdirectory already existed");

    // put two copies of the file into the new subdirectory, dirname + "/" + fname);, dirname + "/duplicate_" + fname);

    // check that new files exist
    if( neko.FileSystem.exists(dirname + "/" + fname) &&
    neko.FileSystem.exists(dirname + "/duplicate_" + fname) )
      neko.Lib.println("copy successfull");
      neko.Lib.println("copy failed");
      neko.Sys.exit(1); // quit

    // delete renamed file if it exists from a previous run
    if( neko.FileSystem.exists(dirname + "/renamed_" + fname) )
      neko.FileSystem.deleteFile(dirname + "/renamed_" + fname);
      neko.Lib.println("deleted old renamed file");

    // rename second new file
    neko.FileSystem.rename(dirname + "/duplicate_" + fname, dirname + "/renamed_" + fname);

    // check that rename was successfull
    if( !neko.FileSystem.exists(dirname + "/duplicate_" + fname) &&
    neko.FileSystem.exists(dirname + "/renamed_" + fname) )
      neko.Lib.println("rename successfull");
      neko.Lib.println("rename failed");
      neko.Sys.exit(1); // quit

    // get directory listing for subdir
    var files = neko.FileSystem.readDirectory(dirname);
    neko.Lib.println("subdirectory listing:");
    for( ff in files)
      neko.Lib.println(" " + dirname + "/" + ff);

    // delete first copy
    neko.FileSystem.deleteFile(dirname + "/" + fname);

    // check that delete was successfull
    if( !neko.FileSystem.exists(dirname + "/" + fname) )
      neko.Lib.println("delete successfull");
      neko.Lib.println("delete failed");
      neko.Sys.exit(1); // quit

Compile and run with:

haxe -neko filesysex.n -main FileSystemExample
neko filesysex

The first time it runs, the output should resemble the output below. The file path and access times will differ. The direction of the path separators will be appropriate for the current operating system.

filename: c:\Haxe\filesystem\input.txt
file exists
Last access time: 2009-04-14 15:26:53
Last modification time: 2007-10-29 12:30:54
Last status change time: 2007-10-29 12:26:00
subdirectory subdir was created
copy successfull
rename successfull
subdirectory listing:
delete successfull

version #12194, modified 2012-01-15 19:11:51 by JLM