File.open() (Method)

Opens a file for reading, writing or appending.

Availability:

JavaScript - 1.1
Netscape Enterprise Server - 2.0
JavaScript syntax:NESmyFile.open(aMode);
Argument list:aModeA mode of opening the file (read, write, append)

This method opens the file that is encapsulated by the File object.

C language programmers will be immediately familiar with this method. It behaves very similarly to the fopen() call in the ANSI C language libraries. The difference is that this method lacks a filename argument which is moved to the constructor function which is called separately.

The following flag values can be used with this method:

FlagDescription
rOpens the file for reading
r+Opens the file for reading and writing
wOpens the file for writing
w+Opens the file for writing and reading
aOpens the file for appending
a+Opens the file for reading and appending
brOpens a Windows binary file for reading
br+Opens a Windows binary file for reading and writing
bwOpen a Windows binary file for writing
bw+Opens a Windows binary file for writing and reading
baOpens a Windows binary file for appending
ba+Opens a Windows binary file for reading and appending

If you are using this in a server-side application (within NES), you should make sure the project locking is activated to avoid file corruption happening if there are multiple simultaneous accesses to the file.

Example code:

   <SERVER>
   // Example file create, open and write taken from
   // Wrox Professional JavaScript
   myFile = new File("file.txt");
   myFile.open("a");
   myFile.writeln("Append this line to the file.");
   myFile.close();
   </SERVER>

See also:File.close(), FileSystem.CreateTextFile(), FileSystem.OpenTextFile(), project.lock()