A Javascript friendly proxy for java.io.File
| Myna.File | A Javascript friendly proxy for java.io.File |
| Functions | |
| File | Creates a new File Object from the supplied MynaPath |
| Properties | |
| javaFile | the underlying java.io.File object |
| size | size in bytes of this file. |
| lastModified | the Date() this file was last modified, or null if this file does not exist |
| fileName | the name of this file, not including it’s path |
| directoryPath | a MynaPath representing the parent directory of this File |
| type | either “file” or “directory” |
| Functions | |
| appendBinary | appends a strings to this file |
| appendString | appends a strings to this file |
| copyTo | copies this file/directory to another location |
| createDirectory | Creates a directory represented by this File, including parent directories as necessary. |
| createTempFile | (static) Creates a uniquely named file. |
| exists | returns true if the filepath this object reperesents currently exists |
| extractZip | Tries to unzip this file into the supplied destination directory, and returns array of file names |
| forceDelete | delete the filepath this object reperesents |
| getChecksum | returns a checksum for this file as a Java byte[] |
| getDirectory | Returns a File representing the parent directory of this File |
| getDirectoryPath | Returns a MynaPath representing the parent directory of this File |
| getFileName | Returns the name of this file, not including it’s path |
| getInputStream | Returns a java.io.InputStream for this object. |
| getOutputStream | Returns a java.io.OutputStream for this object. |
| getLastModified | returns the Date() this file was last modified, or null if this file does not exist |
| getLineIterator | returns an interator object for looping one line at a time over the file without loading the entire file into memory. |
| getSize | returns the size in bytes of this file |
| isDirectory | returns true if this file exists and represents a directory |
| isValidChecksum | returns true if the supplied hex checksum matchs this file |
| listFiles | Returns a Myna.DataSet of File objects contained in this directory. |
| readBinary | returns the contents of this file as a java byte[] array |
| readLine | returns the contents of a specific line number in a file, or null if the line does not exist |
| readLines | returns the contents of this file as an array of strings representing each line |
| readString | returns the text contents of this file |
| renameTo | Gives this file a new name |
| toString | returns the full MynaPath represented by this File |
| writeBinary | write a java byteArray to this file |
| writeString | write a strings to this file |
Myna.File =function ( path )
Creates a new File Object from the supplied MynaPath
| path | MynaPath to a file. This can be be multiple paths to be concatenated together |
var wwwroot = new Myna.File("/")
var unix_server_root = new Myna.File("file:///")
//this will attempt to make a valid path, inserting "/"s between arguments
// and removing any duplicate "/"s
var modules_path = new Myna.File(
$FP.dir,
"plugins/modules",
c2f(controllerName)
)size in bytes of this file. Same result as getSize()
Myna.File.prototype.appendBinary = function( byteArray )
appends a strings to this file
| string | byte[] to append to the file, preserving any existing contents |
The file does not have to exist, but the parent directory does.
Myna.File.prototype.appendString = function( string )
appends a strings to this file
| string | String to append to the file, preserving any existing contents |
The file does not have to exist, but the parent directory does.
Myna.File.prototype.copyTo = function( dest, filter )
copies this file/directory to another location
| dest | MynaPath or File representing the directory to copy to |
| filter | *Optional, default null8 Function. If defined and this file is a directory, the this function will be called with every Myna.File to be copied and should return true to copy the file/directory |
void
Copies a file or whole directory to a new location preserving file dates.
Myna.File.prototype.createDirectory = function( name )
Creates a directory represented by this File, including parent directories as necessary.
| fail_on_exist | Optional default true if true, an exception is thrown if the file or directory already exists. If false, no action is performed on the existing directory |
true if successful
Myna.File.createTempFile=function( prefix, suffix, path )
(static) Creates a uniquely named file.
| prefix | Optional String prefix to the file name. |
| suffix | Optional, default ‘.tmp’ String suffix to the filename |
| path | *Optional, default $server.tempDir |
MynaPath to the created file
To create the new file, the prefix and the suffix may first be adjusted to fit the limitations of the underlying platform. If the prefix is too long then it will be truncated, but its first three characters will always be preserved. If the suffix is too long then it too will be truncated, but if it begins with a period character (‘.’) then the period and the first three characters following it will always be preserved. Once these adjustments have been made the name of the new file will be generated by concatenating the prefix, five or more internally-generated characters, and the suffix.
Myna.File.prototype.exists = function()
returns true if the filepath this object reperesents currently exists
Myna.File.prototype.extractZip = function( dest )
Tries to unzip this file into the supplied destination directory, and returns array of file names
| dest | MynaPath or Myna.File representing the destination directory. The path does not have to exist, but if it does, it must be a directory. |
Array of MynaPath entries representing the extracted files
Myna.File.prototype.forceDelete = Myna.File.prototype.deleteFile = function()
delete the filepath this object reperesents
If this file is a directory, its contents are recursively deleted as well. Throws java exception if the file cannot be deleted.
Myna.File.prototype.getChecksum = function( type )
returns a checksum for this file as a Java byte[]
| type | Optional, default “SHA-256” Any valid digest algorithm, see below |
This function returns a Hex string representing the state of the file. This can be validated via Myna.File.isValidChecksum to confirm that a file has not been altered.
Myna.File.prototype.getFileName=function()
Returns the name of this file, not including it’s path
Myna.File.prototype.getInputStream=function()
Returns a java.io.InputStream for this object.
If this stream is not explicitly closed, then it will be closed at the end of the request.
Myna.File.prototype.getOutputStream=function()
Returns a java.io.OutputStream for this object.
If this stream is not explicitly closed, then it will be closed at the end of the request.
Myna.File.prototype.getLastModified = function()
returns the Date() this file was last modified, or null if this file does not exist
Myna.File.prototype.getLineIterator= function()
returns an interator object for looping one line at a time over the file without loading the entire file into memory. This will lock the file until the end of the request unless to call close() on the returned iterator when finished with it.
var file = new Myna.File("path/to/file");
for (line in file.getLineIterator()) {
Myna.print(line+"<br>");
}
Myna.File.prototype.isDirectory = function( name )
returns true if this file exists and represents a directory
Myna.File.prototype.isValidChecksum = function( checksum, type )
returns true if the supplied hex checksum matchs this file
| checksum | java byte[] representing a checksum |
| type | Optional, default “SHA-256” Any valid digest algorithm, see below |
This function returns a Hex string representing the state of the file. This can be validated via Myna.File.isValidChecksum to confirm that a file has not been altered.
Myna.File.prototype.listFiles=function( filter, recursive, maxFiles )
Returns a Myna.DataSet of File objects contained in this directory.
| filter | Optional, default: null Either a comma separated list of file extensions to list, ex: “css,js”, OR a filter function( see Filter Function below). If null all files are returned. |
| recursive | Optional, default: false If true, all sub-directories will be searched as well. If this is a filter function, then only directories for which this function returns true will be recursed |
| maxFiles | Optional, default: null If defined, this will be the maximum number of files to examine. Can be used to prevent excessive recursion Filter Function: The filter function takes a single argument, a Myna.File instance. This function should return true if the file should be included in the result set or false otherwise. Throwing an exception in this function can be used used to exit early |
//Find all the files and directories, but not sub-directories in the Myna root:
var files = new Myna.File("/").listFiles();
//Find all the code files in the current directory, or lower
var files = new Myna.File(".").listFiles("sjs,ejs,ws",true);
//Find all files in the tmp directory older than 30 days
var files = new Myna.File("file:/tmp").listFiles(function(f){
var isOld =f.lastModified < new Date().add(Date.DAY,-30);
return isOld && !f.isDirectory();
},true);
//Find all code files, in all subdirectories except for in .svn directories
var files = new Myna.File(".").listFiles("sjs,ejs,ws",function(d){
return d.fileName != ".svn";
});
Myna.File.prototype.readBinary = function()
returns the contents of this file as a java byte[] array
Myna.File.prototype.readLine = function( lineNumber )
returns the contents of a specific line number in a file, or null if the line does not exist
| lineNumber | Optional, default 1 The 1-indexed line number to read from the file |
Myna.File.prototype.readLines = function( start, end )
returns the contents of this file as an array of strings representing each line
| start | Optional, default 0 The first 1-indexed line number from which to read from the file. If negative, then this is “minus n lines from end” |
| end | Optional, default end of file The last 1-indexed line number from which to read from the file. If negative, then this is this is “minus n lines from end of file” |
var f = new Myna.File("lines.txt")
//create a 20 line file with line numbers
Array.dim(20).forEach(function(d,i){
f.appendString(i);
})
f.readLines(); //returns every line (20 lines)
f.readLines(2); //returns second line forward (19 lines)
f.readLines(2,2); //returns only the second line (1 line) See <readLine>
f.readLines(2,1); //returns empty array, because end is before start (0 lines)
f.readLines(2,5); //returns lines 2-5 inclusive (4 lines)
f.readLines(2,-5); //returns lines 2-15 inclusive or " the second line though EOF -5" (14 lines)
f.readLines(-2); //returns last 2 lines or (2 lines)
f.readLines(-2,-1); //returns second-to-last line (1 lines)
f.readLines(-2,-5); //returns empty array, because end is before start (0 lines)
f.readLines(-2,5); //returns line 3 and 4, or "the 2 lines before 5" (2 lines)
f.readLines(25); //returns empty array, because start is beyond end of file (0 lines)
f.readLines(1,25); //returns only existing 20 lines, because end is beyond end of file (20 lines)
Myna.File.prototype.readString = function()
returns the text contents of this file
text contents of this file
Myna.File.prototype.renameTo = function( name )
Gives this file a new name
| name | New name for the file, not including directory |
true if successful
Creates a new File Object from the supplied MynaPath
Myna.File =function ( path )
appends a strings to this file
Myna.File.prototype.appendBinary = function( byteArray )
appends a strings to this file
Myna.File.prototype.appendString = function( string )
copies this file/directory to another location
Myna.File.prototype.copyTo = function( dest, filter )
Creates a directory represented by this File, including parent directories as necessary.
Myna.File.prototype.createDirectory = function( name )
(static) Creates a uniquely named file.
Myna.File.createTempFile=function( prefix, suffix, path )
returns true if the filepath this object reperesents currently exists
Myna.File.prototype.exists = function()
Tries to unzip this file into the supplied destination directory, and returns array of file names
Myna.File.prototype.extractZip = function( dest )
delete the filepath this object reperesents
Myna.File.prototype.forceDelete = Myna.File.prototype.deleteFile = function()
returns a checksum for this file as a Java byte[]
Myna.File.prototype.getChecksum = function( type )
Returns a File representing the parent directory of this File
Myna.File.prototype.getDirectory=function()
Returns a MynaPath representing the parent directory of this File
Myna.File.prototype.getDirectoryPath=function()
Returns the name of this file, not including it’s path
Myna.File.prototype.getFileName=function()
Returns a java.io.InputStream for this object.
Myna.File.prototype.getInputStream=function()
Returns a java.io.OutputStream for this object.
Myna.File.prototype.getOutputStream=function()
returns the Date() this file was last modified, or null if this file does not exist
Myna.File.prototype.getLastModified = function()
returns an interator object for looping one line at a time over the file without loading the entire file into memory.
Myna.File.prototype.getLineIterator= function()
returns the size in bytes of this file
Myna.File.prototype.getSize = function()
returns true if this file exists and represents a directory
Myna.File.prototype.isDirectory = function( name )
returns true if the supplied hex checksum matchs this file
Myna.File.prototype.isValidChecksum = function( checksum, type )
Returns a Myna.DataSet of File objects contained in this directory.
Myna.File.prototype.listFiles=function( filter, recursive, maxFiles )
returns the contents of this file as a java byte[] array
Myna.File.prototype.readBinary = function()
returns the contents of a specific line number in a file, or null if the line does not exist
Myna.File.prototype.readLine = function( lineNumber )
returns the contents of this file as an array of strings representing each line
Myna.File.prototype.readLines = function( start, end )
returns the text contents of this file
Myna.File.prototype.readString = function()
Gives this file a new name
Myna.File.prototype.renameTo = function( name )
returns the full MynaPath represented by this File
Myna.File.prototype.toString = function()
write a java byteArray to this file
Myna.File.prototype.writeBinary = function( data )
write a strings to this file
Myna.File.prototype.writeString = function( string )