File access library

Provide functionality as read and write files.

This library can be initialized

var File = require('file');

Create and release file instance

Use constructor for create instance of file.

new File (filePath);

String filePath - is complete path and filename to your file.

When you need not instance of file for next time, you have to release it.

close ();

This function releases all resources and file descriptor.

Warning Please, avoid to multiple access to one file at the same time!

File size, create, remove and exists

Number size ();

Function returns size of file in bytes. File has to exist!

create ();

Function creates file in filesystem and with 0 length.

remove ();

Function removes file from filesystem. File has to exist!

Boolean exists ();

Function returns true or false if file exists or not.

Opening file for read or write

openForRead ();

Function opens file for reading from begin of file. File has to exist!

openForWrite ();

Function opens file for writing from begin of file. Please, use create() before open, when file doesn't exist.

openForAppend ();

Function opens file for writing to end of file. File has to exist!

Reading file content

String readString ();

Number maxLimit - number of read characters.

Function reads file to string. Parameter maxLimit sets maximal length of readed string. Without this parameter, maxLimit is 65535 bytes.

ByteBuffer readByteBuffer ();

Number maxLimit - number of read characters.

Function reads file to ByteBuffer object. Parameter maxLimit sets maximal length of readed buffer. Without this parameter, maxLimit is 65535 bytes.

Number readByte ();

Function reads one byte from file.

Number readShort ();

Function reads two bytes from file.

Number readInt ();

Function reads four bytes from file.

Number readLong ();

Function reads eight bytes from file.

Number readFloat ();

Function reads four bytes from file as number with decimal point.

Number readDouble ();

Function reads eight bytes from file as number with decimal point.

Writing file content

writeString (text);

String text

Function writes string to file.

writeByteBuffer(data);

ByteBuffer data

Function writes content of ByteBuffer to file.

writeByte(num);

Number num

Function writes number as one byte to file.

writeShort (num);

Number num

Function writes number as two bytes to file.

writeInt(num)

Number num

Function writes number as four bytes to file.

writeLong(num);

Number num

Function writes number as eight bytes to file.

writeFloat(num);

Number num

Function writes number as four bytes with decimal point to file.

writeDouble(num);

Number num

Function writes number as eight bytes with decimal point to file.

File example

var File = require('file');
var sys = require('system');
//create file object
var f1 = new File("a:/program.js");
if (f1.exists()) {
    //file exists, open for read
    f1.openForRead();
    var fileLimit = 100;
    var text = f1.readString(fileLimit);
    sys.println("first "+fileLimit+" bytes from file: "+text);
}

//release file source
f1.close();

var f2 = new File("a:/test.bin");
if (!f2.exists()){
    sys.println("File not exists!");
    f2.openForWrite();
    f2.writeByte(63);
    f2.writeShort(1);
    f2.writeInt(1);
    f2.writeFloat(1.8);
    f2.writeDouble(1.2);
    f2.writeString("hello world!");
    f2.close();
}
f2 = new File("a:/test.bin");
if (f2.exists()) {
    f2.openForAppend();
    f2.writeString("appended line!");
}
f2.close();

f2 = new File("a:/test.bin");
if (f2.exists()) {
    sys.println("File exists, reading..");
    f2.openForRead();
    sys.println(f2.readByte());
    sys.println(f2.readShort());
    sys.println(f2.readInt());
    sys.println(f2.readFloat());
    sys.println(f2.readDouble());
    sys.println(f2.readString(12));
}
f2.close();
f2 = new File("a:/test.bin");

if (f2.exists()) {
    f2.remove();
}
f2.close();

results matching ""

    No results matching ""