|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Object | +--File
Class representing files and directories.
Objects of this class can refer to regular files, directories or archives of files. In the two latter cases, the object contains references to its subdirectories and files.
The fileSystem.mountPoint
property and any mounted directories are File
objects. You
may make a File
object by calling the resolve()
method on any of these.
The File
class is special in that it doubles as an array-like object containing the File
objects it refers to.
So if, a directory contains a series of files and directories, you can do the
following:
var dir = mp.resolve('path/to/dir'); //Get a File object referring to the directory
dir.refresh(); //Load the contents of the directory
for ( var i = 0, file; file = dir[i]; i++ )
{
opera.postError(file.name);
}
Field Summary | |
Date |
created
The time and date this File was created. |
boolean |
exists
Whether or not this File exists in the physical file system. |
int |
fileSize
The number of bytes in this File. |
boolean |
isArchive
Whether or not this File is a compressed archive, like a zip or gzip file. |
boolean |
isDirectory
Whether or not this File is a directory. |
boolean |
isFile
Whether or not this File is a regular file. |
boolean |
isHidden
Whether or not this File is hidden in the underlying file system. |
int |
length
The number of files and directories referenced by this File. |
int |
maxPathLength
The maximum number of characters a path reference can contain. |
Object |
metaData
Meta data for this file. |
Date |
modified
The time and date this File was last modified. |
String |
name
The name of this File as a URL encoded String. |
String |
nativePath
The path to this File in the physical file system. |
File |
parent
The parent File of this File, or null if it has no parent. |
String |
path
The path to this File in the virtual file system as a URL encoded String. |
boolean |
readOnly
Whether or not this File is read only. |
Constructor Summary | |
File()
This class has no public constructor. |
Method Summary | |
File
|
copyTo( <File> path, <boolean> overwrite, <Function> callback )
Copy this File to the given File path. |
File
|
createDirectory( <File> directory )
Create a new directory. |
boolean
|
deleteDirectory( <File> directory, <boolean> recursive )
Delete the given directory. |
boolean
|
deleteFile( <File> file )
Delete the given file. |
File
|
moveTo( <File> path, <boolean> overwrite, <Function> callback )
Move this File to the given File path. |
FileStream
|
open( <File> path, <int> mode )
Open a File for reading or writing. |
void
|
refresh()
Refresh the content in this File. |
File
|
resolve( <String> path )
Resolve a path to a file. |
String
|
toString()
String representation of this File. |
Field Detail |
Date created
boolean exists
resolve()
or browseForSave()
,
may in some cases not exist in the file system.
int fileSize
length
property to find out how
many files the directory contains.
boolean isArchive
Note that archives will also have the isDirectory
and isFile
properties set. You may both use resolve()
to resolve files inside the archive, or open
the archive file using open()
.
Currently only a subset of the ZIP format is currently supported as archives.
boolean isDirectory
boolean isFile
boolean isHidden
int length
This property is used for array style lookup. If the File
object is a regular file,
its length is 0. Use the fileSize
property to get the size of regular
files in bytes.
For directories or archives, this property is 0 until refresh()
is called,
except for mount point File
objects that are already loaded.
int maxPathLength
c:\foo\bar
is mounted as bar
, and assuming
the operating system has a maximum path length of 128, the maxPathLength
property
of the File would be 128 - 10 = 110.
Object metaData
null.
Date modified
String name
Anything that occurs after the last '/' in the path of this File. If the file has the path
/foo/bar
, the name is bar
. There is no trailing path separator if
this File is a directory.
String nativePath
The full path of this File in the physical file system, including trailing slash or backslash
for directories. If you mount a directory c:\foo\
as foo
and this directory
contains a file bar.txt
, the nativePath
of this File will be c:\foo\bar.txt
.
Note that the path separator of the underlying operating system is used in the path.
For the mount points mounted by mountSystemDirectory()
, and for all files under them,
this property will be empty to avoid exposing system information to the application.
This property is not URL encoded, i.e. it is not modified in any way from how the underlying file system would represent the path.
File parent
null
.
String path
The full path of this File in the virtual file system, starting with the name of the mount point and including the full file name of this file or directory. There is no trailing path separator if this File is a directory.
boolean readOnly
mountSystemDirectory()
are
not writeable. Otherwise, the physical file system determines whether or not
the File is writeable.
Constructor Detail |
File()
Method Detail |
File copyTo( <File> path, <boolean> overwrite, <Function> callback )
Calling this function will copy all the contents of this File to the given target location, given
as either a File
object or a String containing the path.
If the target location exists, this operation will fail with an exception. Use the
overwrite
argument to replace existing files in target location.
Supplying the optional callback
will make the operation asynchronous, and the function
will immediately return a File
object representing the copy of the File, regardless of whether the
operation is complete. The callback is called when the copy operation is complete, with the copy of the
File as an argument. If the operation fails, the callback is called with a null
argument.
path
- The target location to copy this File to, as either a File or an URL encoded String with the path.
overwrite
- Whether or not to overwrite any content present in the target path. Optional, default false.
callback
- Function to call when the copy is completed. Optional.
overwrite
argument is false
.File createDirectory( <File> directory )
Create a new directory using either a File object or a URL encoded String with a path to the new directory. All non-existing parent directories are created along with it.
file = mountPoint.createDirectory(somePath);
file = mountPoint.createDirectory(mountPoint.resolve(somePath));
directory
- File referring to the desired directory, or a URL encoded String with the path to the directory.
boolean deleteDirectory( <File> directory, <boolean> recursive )
If the recursive
argument is given as true
, this method will attempt to delete the
directory and all of its content. If deleting individual files or directories in it fails, the method will continue
to delete the rest of the content.
If the entire directory and all of its content is deleted, the method will return true
. If parts
of the content, and thus also the directory itself could not be deleted, the method will return
false
.
directory
- File representing the directory or a URL encoded String with the path to the directory to delete.
recursive
- Whether or not to recursively delete any content references by this File. Optional, default false.
boolean deleteFile( <File> file )
File
object or a URL encoded String with a path and deletes the
referenced file.file
- File representing the directory or a URL encoded String with the path to the file to delete.
File moveTo( <File> path, <boolean> overwrite, <Function> callback )
Calling this function will move all the contents of this File to the given File target location.
If the target location exists, this operation will fail with an exception. Use the
overwrite
argument to replace existing files in target location.
Supplying the optional callback
will make the operation asynchronous, and the function
will immediately return a File
object representing the new File regardless of whether the
operation is complete. The callback is called with the new File as an argument. If the
operation fails, the callback is called with a null
argument.
path
- The target location to move this File to, as either a File or an URL encoded String with the path.
overwrite
- Whether or not to overwrite any content present in the target path. Optional, default false.
callback
- Function to call when the move is competed. Optional.
overwrite
argument is false
.FileStream open( <File> path, <int> mode )
If the path argument is given as null
, this File will be opened.
The file can be opened in read, write, append or update mode, represented by the constants in opera.io.filemode.
The mode argument is similar to PHPs fopen()
, but implemented as constants which can be combined through a bitwise OR,
for example as opera.io.filemode.APPEND | opera.io.filemode.READ
.
If the file does not exist when opened in WRITE or APPEND mode, it is immediately created. The entire path to the file is created if this does not exist.
If the file does not exist when opened in READ or UPDATE mode, a FILE_NOT_FOUND_ERR is thrown.
The previous version of the API accepted a string equal to the ones described below. This is now deprecated in favor of the constants in opera.io.filemode.
The the following is an extract from http://no2.php.net/fopen and explains possible combinations:
If a file is opened in an invalid mode, for example opening a read-online file in WRITE mode, a SECURITY_ERR is thrown.
Note that opera.io.filemode.UPDATE represents 'r+'.
path
- File object to read, or a URL encoded String with the path to the file to read.
mode
- Whether to open the file for reading, writing, appending or a combination.
void refresh()
File
object. You need to call this method
again to see the changes.File resolve( <String> path )
This function will take a URL encoded String with a path and attempt to resolve the path.
If the path is valid, a File
object representing it is returned. The File may
point to a non-existing file or directory, as long as the path is valid. The
resulting File can, for example, be used with the File.createDirectory() method.
If the path is invalid, i.e. pointing to something outside en existing sandbox, an exception is thrown. You may resolve paths with characters that are not recommended and get a File, though exceptions will typically be thrown if you attempt to read from or write to such files.
path
- URL encoded String with path of the file to resolve.
String toString()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |