Object built-in object

Object File

File operation object, used to read and write binary files

File operation objects are used to operate on binary files. You can use the fs module to open and create files:

1
var f = fs.openFile('test.txt');

Inheritance

Member attributes

name

String, query the current file name

1
readonly String File.name;

fd

Integer, query the current file descriptor

1
readonly Integer File.fd;

Member function

chmod

Query the access permissions of the current file, Windows does not support this method

1
File.chmod(Integer mode) async;

Call parameters:

  • mode : Integer, specify the set access authority

seek

Move the current operating position of the file

1 2
File.seek(Long offset, Integer whence);

Call parameters:

  • offset : Long, specify a new position
  • whence : Integer, specify the position reference, allowed values: SEEK_SET, SEEK_CUR, SEEK_END

tell

Query the current position of the stream

1
Long File.tell();

Return result:

  • Long , returns the current position of the stream

rewind

Move the current position to the beginning of the stream

1
File.rewind();

size

Query stream size

1
Long File.size();

Return result:

  • Long , return stream size

readAll

Read all remaining data from the stream

1
Buffer File.readAll() async;

Return result:

  • Buffer , returns the data read from the stream, if there is no data to read, or the connection is interrupted, it returns null

truncate

Modify the file size, if the new size is smaller than the original size, the file is truncated

1
File.truncate(Long bytes) async;

Call parameters:

  • bytes : Long, the new file size

eof

Check if the file is at the end

1
Boolean File.eof();

Return result:

  • Boolean , return True to indicate the end

stat

Query the basic information of the current file

1
Stat File.stat() async;

Return result:

  • Stat , return Stat object description file information

read

Read data of the specified size from the stream

1
Buffer File.read(Integer bytes = -1) async;

Call parameters:

  • bytes : Integer, specify the amount of data to be read, the default is to read data blocks of random size, the size of the data read depends on the device

Return result:

  • Buffer , returns the data read from the stream, if there is no data to read, or the connection is interrupted, it returns null

write

Write the given data to the stream

1
File.write(Buffer data) async;

Call parameters:

  • data : Buffer , given the data to be written

flush

Write the contents of the file buffer to the physical device

1
File.flush() async;

close

Close the current stream object

1
File.close() async;

copyTo

Copy stream data to the target stream

1 2
Long File.copyTo(Stream stm, Long bytes = -1) async;

Call parameters:

  • stm : Stream , target stream object
  • bytes : Long, the number of bytes copied

Return result:

  • Long , returns the number of bytes copied

toString

Returns the string representation of the object, generally returns "[Native Object]", the object can be re-implemented according to its own characteristics

1
String File.toString();

Return result:

  • String , returns the string representation of the object

toJSON

Returns the JSON format representation of the object, generally returns a collection of readable attributes defined by the object

1
Value File.toJSON(String key = "");

Call parameters:

  • key : String, unused

Return result:

  • Value , returns a value containing JSON serializable