server-side JavaScript: Example 1: File class

Discussion in 'Javascript' started by GVDC, Jun 28, 2008.

  1. GVDC

    GVDC Guest

    Example server-side JavaScript Web script, working with files on server

    //File class;
    //Open file, lock, write text to file, then read and print data from file
    //
    printf("<html><body>");

    var fileobj = new File("myfile.txt"); //File class

    //open for RW, truncate and if not existing create mode 0644
    if ( fileobj.open("rwtc", 0644)==true ) {
    //set exclusive lock
    if ( fileobj.lock("x")==true ) {

    //write ok
    if ( fileobj.write("abc text text more text")!=(-1) ) {
    //rewind to beggining
    fileobj.seek(0);

    //read (with no arg) whole file
    var strfromfile = fileobj.read();

    //data from file successfully read
    if ( strfromfile!=null ) {
    printf("Data read from file:<br>");
    printf(strfromfile, "<br>");
    }


    }

    }
    //close file
    fileobj.close();

    }
    //error opening file
    else {
    //ie. invalid path, no permission etc.
    //Error open file [errno 2] No such file or directory
    printf("Error open file [errno " ,Server.errno(), "] ", Server.errstr(), "<br>");
    }
    printf("</body></html>");





    --
    GVDC, Jun 28, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Qw==?=
    Replies:
    1
    Views:
    3,209
    Hermit Dave
    Jan 16, 2004
  2. Shawn Milo
    Replies:
    20
    Views:
    577
    Bruno Desthuilliers
    Mar 4, 2007
  3. GVDC
    Replies:
    0
    Views:
    363
  4. GVDC
    Replies:
    0
    Views:
    143
  5. GVDC
    Replies:
    1
    Views:
    140
    Thomas 'PointedEars' Lahn
    Jun 28, 2008
Loading...

Share This Page