File

Core object New in package 162.3

Created by

The File constructor:

new File( path [, mode] )

Parameters

Parameter Description
path File path, in local or UNC format.
mode File open mode, as comma-separated list of the following string constants: read, write, readWrite, replace

Description

File object provides means to read from, write to and create new files in the local file system.

Property Summary

Property Description
name Name of the file.
path Full path to the file.
length Size of the file. New in package 167
mode File open mode.
isOpen true if the file has been successfully opened and is still open.
position Current position in the file.

Method Summary

Method Description
read( n ) Reads n bytes from the file and returns them as a string. If there are less than n bytes left to read, then the available bytes are returned.
readAll() Reads the whole contents of the file at once. New in package 164.5
write( data[, options] ) Writes string data to the file, returns the number of bytes actually written. Stream object (e.g. Archive, Http.get, Http.post) can be passed as data - in this case write operation is asynchronous, return value is 0, options.success and options.error may specify callback functions to be called when writing is done. Error callback receives error's descriptions as the first argument.
seek( offset ) Moves the current file position according to the specified offset. Returns the new position.
close() Closes the file.
Read/written data is represented as JavaScript string but this string has "binary" format - every element of this string represents a single byte of data. I.e., data.charCodeAt(index) will be always in range [0, 255]. So if read data is representing text then it should be decoded according to used encoding. E.g. if file has UTF8-encoded content then read data should be decoded using function like this:

Examples

Local file (Windows)

Network file (OSX and Windows)

With callbacks

See Also

MountVolumeForPath
Archive
Http.get
Http.post

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.