Myna.File

A Javascript friendly proxy for java.io.File

Summary
Myna.FileA Javascript friendly proxy for java.io.File
Functions
FileCreates a new File Object from the supplied MynaPath
Properties
javaFilethe underlying java.io.File object
sizesize in bytes of this file.
lastModifiedthe Date() this file was last modified, or null if this file does not exist
fileNamethe name of this file, not including it’s path
directoryPatha MynaPath representing the parent directory of this File
typeeither “file” or “directory”
Functions
createTempFilestatic Creates a uniquely named file.
mkDirstatic Makes a directory from a path, and returns a File instance referencing that path
appendBinaryappends a strings to this file
appendStringappends a strings to this file
copyTocopies this file/directory to another location
createDirectoryCreates a directory represented by this File, including parent directories as necessary.
existsreturns true if the filepath this object reperesents currently exists
extractZipTries to unzip this file into the supplied destination directory, and returns array of file names
forceDeletedelete the filepath this object reperesents
getChecksumreturns a checksum for this file as a Java byte[]
getDirectoryReturns a File representing the parent directory of this File
getDirectoryPathReturns a MynaPath representing the parent directory of this File
getFileNameReturns the name of this file, not including it’s path
getInputStreamReturns a java.io.InputStream for this object.
getOutputStreamReturns a java.io.OutputStream for this object.
getLastModifiedreturns the Date() this file was last modified, or null if this file does not exist
getLineIteratorreturns an interator object for looping one line at a time over the file without loading the entire file into memory.
getSizereturns the size in bytes of this file
isDirectoryreturns true if this file exists and represents a directory
isValidChecksumreturns true if the supplied hex checksum matchs this file
listFilesReturns a Myna.DataSet of File objects contained in this directory.
readBinaryreturns the contents of this file as a java byte[] array
readLinereturns the contents of a specific line number in a file, or null if the line does not exist
readLinesreturns the contents of this file as an array of strings representing each line
readStringreturns the text contents of this file
renameToGives this file a new name
toStringreturns the full MynaPath represented by this File
writeBinarywrite a java byteArray to this file
writeStringwrite a strings to this file

Functions

File

Myna.File =function (path)

Creates a new File Object from the supplied MynaPath

Parameters

pathMynaPath to a file.  This can be be multiple paths to be concatenated together

Example

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)
)

Properties

javaFile

the underlying java.io.File object

size

size in bytes of this file.  Same result as getSize()

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

createTempFile

Myna.File.createTempFile=function(prefix,
suffix,
path)

static Creates a uniquely named file.

Parameters

prefixOptional String prefix to the file name.
suffixOptional, default ‘.tmp’ String suffix to the filename
path*Optional, default $server.tempDir

Returns

MynaPath to the created file

Detail

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.

var path = Myna.createTempFile("sql",".tmp",$applciation.directory)
new Myna.File(path).writeString(<ejs>
select *
from employees
</ejs>)

mkDir

Myna.File.mkDir=function(path)

static Makes a directory from a path, and returns a File instance referencing that path

Parameters

pathMynaPath for the directory to create

Returns

File instance to the created directory

...
reportFile.copyTo(
//creates the directory if necessary
Myna.File.mkDir($application.directory + "/reports")
)

See Also

appendBinary

Myna.File.prototype.appendBinary = function(byteArray)

appends a strings to this file

Parameters

stringbyte[] to append to the file, preserving any existing contents

Note

The file does not have to exist, but the parent directory does.

appendString

Myna.File.prototype.appendString = function(string)

appends a strings to this file

Parameters

stringString to append to the file, preserving any existing contents

Note

The file does not have to exist, but the parent directory does.

copyTo

Myna.File.prototype.copyTo = function(dest,
filter)

copies this file/directory to another location

Parameters

destMynaPath 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

Returns

void

Detail

Copies a file or whole directory to a new location preserving file dates.

If this File is a file

  • If the destination is a file, then the directory holding the destination file is created, if neccesary and the destination file is overwritten, if it exists.
  • If the destination is a directory, then the this File will by copied into that directory

If this File is a directory

  • This directory and all its child directories and files are copied to the specified destination.
  • The destination is the new location and name of the directory.
  • The destination directory is created if it does not exist.
  • If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.

createDirectory

Myna.File.prototype.createDirectory = function()

Creates a directory represented by this File, including parent directories as necessary.

Returns

true if successful

See Also

exists

Myna.File.prototype.exists = function()

returns true if the filepath this object reperesents currently exists

extractZip

Myna.File.prototype.extractZip = function(dest)

Tries to unzip this file into the supplied destination directory, and returns array of file names

Parameters

destMynaPath or Myna.File representing the destination directory.  The path does not have to exist, but if it does, it must be a directory.

Returns

Array of MynaPath entries representing the extracted files

forceDelete

Myna.File.prototype.forceDelete = Myna.File.prototype.deleteFile = function()

delete the filepath this object reperesents

Detail

If this file is a directory, its contents are recursively deleted as well.  Throws java exception if the file cannot be deleted.

getChecksum

Myna.File.prototype.getChecksum = function(type)

returns a checksum for this file as a Java byte[]

Parameters

typeOptional, default “SHA-256” Any valid digest algorithm, see below

Sun Java Digest Algorithms (versions 5/6)

  • MD2
  • MD5
  • SHA-1
  • SHA-256
  • SHA-384
  • SHA-512

Detail

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.

See

getDirectory

Myna.File.prototype.getDirectory=function()

Returns a File representing the parent directory of this File

Returns

a File representing the parent directory of this File

getDirectoryPath

Myna.File.prototype.getDirectoryPath=function()

Returns a MynaPath representing the parent directory of this File

Returns

a MynaPath representing the parent directory of this File

getFileName

Myna.File.prototype.getFileName=function()

Returns the name of this file, not including it’s path

getInputStream

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.

getOutputStream

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.

getLastModified

Myna.File.prototype.getLastModified = function()

returns the Date() this file was last modified, or null if this file does not exist

getLineIterator

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.

Example

var file = new Myna.File("path/to/file");
for (line in file.getLineIterator()) {
Myna.print(line+"<br>");
}

getSize

Myna.File.prototype.getSize = function()

returns the size in bytes of this file

isDirectory

Myna.File.prototype.isDirectory = function(name)

returns true if this file exists and represents a directory

isValidChecksum

Myna.File.prototype.isValidChecksum = function(checksum,
type)

returns true if the supplied hex checksum matchs this file

Parameters

checksumjava byte[] representing a checksum
typeOptional, default “SHA-256” Any valid digest algorithm, see below

Sun Java Digest Algorithms (versions 5/6)

  • MD2
  • MD5
  • SHA-1
  • SHA-256
  • SHA-384
  • SHA-512

Detail

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.

See

listFiles

Myna.File.prototype.listFiles=function(filter,
recursive,
maxFiles)

Returns a Myna.DataSet of File objects contained in this directory.

Parameters

filterOptional, 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.
recursiveOptional, 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
maxFilesOptional, 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

Examples

//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";
});

See

  • <function.filter>

readBinary

Myna.File.prototype.readBinary = function()

returns the contents of this file as a java byte[] array

readLine

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

Paramters

lineNumberOptional, default 1 The 1-indexed line number to read from the file

readLines

Myna.File.prototype.readLines = function(start,
end)

returns the contents of this file as an array of strings representing each line

Parameters

startOptional, 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
endOptional, 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”

Examples

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)

readString

Myna.File.prototype.readString = function()

returns the text contents of this file

Returns

text contents of this file

renameTo

Myna.File.prototype.renameTo = function(name)

Gives this file a new name

Parameters

nameNew name for the file, not including directory

Returns

true if successful

toString

Myna.File.prototype.toString = function()

returns the full MynaPath represented by this File

writeBinary

Myna.File.prototype.writeBinary = function(data)

write a java byteArray to this file

Parameters

datajava byte[] array to write to the file, overwriting any existing contents

Note

The file does not have to exist, but the parent directory does.

writeString

Myna.File.prototype.writeString = function(string)

write a strings to this file

Parameters

stringString to write to the file, overwriting any existing contents

Note

The file does not have to exist, but the parent directory does.

Myna.File =function (path)
Creates a new File Object from the supplied MynaPath
A MynaPath is a file URI String with certain automatic translations.
Myna.File.createTempFile=function(prefix,
suffix,
path)
static Creates a uniquely named file.
Myna.File.mkDir=function(path)
static Makes a directory from a path, and returns a File instance referencing that path
Myna.File.prototype.appendBinary = function(byteArray)
appends a strings to this file
Myna.File.prototype.appendString = function(string)
appends a strings to this file
Myna.File.prototype.copyTo = function(dest,
filter)
copies this file/directory to another location
Myna.File.prototype.createDirectory = function()
Creates a directory represented by this File, including parent directories as necessary.
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
Myna.File.prototype.forceDelete = Myna.File.prototype.deleteFile = function()
delete the filepath this object reperesents
Myna.File.prototype.getChecksum = function(type)
returns a checksum for this file as a Java byte[]
Myna.File.prototype.getDirectory=function()
Returns a File representing the parent directory of this File
Myna.File.prototype.getDirectoryPath=function()
Returns a MynaPath representing the parent directory of this File
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.
Myna.File.prototype.getOutputStream=function()
Returns a java.io.OutputStream for this object.
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.
Myna.File.prototype.getSize = function()
returns the size in bytes of this file
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
Myna.File.prototype.listFiles=function(filter,
recursive,
maxFiles)
Returns a Myna.DataSet of File objects contained in this directory.
A specialized array for working with tabular data
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
Myna.File.prototype.readLines = function(start,
end)
returns the contents of this file as an array of strings representing each line
Myna.File.prototype.readString = function()
returns the text contents of this file
Myna.File.prototype.renameTo = function(name)
Gives this file a new name
Myna.File.prototype.toString = function()
returns the full MynaPath represented by this File
Myna.File.prototype.writeBinary = function(data)
write a java byteArray to this file
Myna.File.prototype.writeString = function(string)
write a strings to this file
A Javascript friendly proxy for java.io.File