File
概述
文件接口。盒子有一个简易的文件系统,里面有32个文件,每个文件最大为2K字节,文件名固定为"1"到"32"。
增强版硬件外挂有一个16M的闪存,文件名表示为"0:\文件名",暂不支持目录,不支持长文件名,文件名最长8个字节,后缀名最长3个字节。
一般闪存的擦写寿命为10万次,写文件不应该太过频繁。如果设备按3年寿命估算,每小时写同一个文件次数应少于4次。
常量
文件存储位置
文件存储位置定义:
File.INTER
File.FLASH
常量 | 说明 |
---|---|
File.INTER | 内部文件系统 |
File.FLASH | 外部文件系统,目前只有增强版硬件支持 |
函数
open
打开文件,同一时间内最多只能打开两个文件。
File.open(filename)
参数 必选 类型 说明 filename true String 要打开的文件名 return int 文件句柄,大于或等于0为有效句柄,小于0则打开出错
close
关闭文件。
File.close(filehandle)
参数 必选 类型 说明 filehandle true int 文件句柄 return null 无返回值
read
读取数据。
File.read(filehandle, len)
参数 必选 类型 说明 filehandle true int 文件句柄 len true int 读取数据长度 return Bytes 读取到的数据,数据长度有可能小于len;返回nil表示读取错误
write
写数据。
File.write(filehandle, data_string | data_bytes)
参数 必选 类型 说明 filehandle true int 文件句柄 data_string ture String 写入的数据,用于写入字符串 data_bytes true Bytes 写入的数据,用于写入字节流 return int 已写入的数据长度,该长度可能小于原数据长度;小于0则表示写错误
len
获取文件长度。
File.len(filehandle)
参数 必选 类型 说明 filehandle true int 文件句柄 return int 文件的长度;小于0则表示错误
seek
移动文件指针。
File.seek(filehandle, pos)
参数 必选 类型 说明 filehandle true int 文件句柄 pos true int 要移动的指针位置,不能大于现在文件长度 return int 移动后的指针位置;小于0则表示错误
remove
删除文件。
File.remove(filename)
参数 必选 类型 说明 filename true String 要删除的文件名 return int 0表示删除成功,小于0则打开出错
rename
重命名文件,只对于外部文件系统有效。
File.rename(oldfilename, newfilename)
参数 必选 类型 说明 oldfilename true String 旧文件名 newfilename true String 新文件名 return int 0表示删除成功,小于0则打开出错
readfile
快速读取文件,此方法尽量不要用在几k以上的大文件,因为需要占用文件一样大小的内存。
File.readfile(filename [,len])
参数 必选 类型 说明 filename true String 要读取的文件名 len false int 读取数据长度,忽略则整个文件读出 return Bytes 读取到的数据;返回nil表示读取错误
writefile
快速写入文件,注意内部文件最大只支持2K。
File.writefile(filename, data_string | data_bytes)
参数 必选 类型 说明 filename true String 要写入的文件名 data_string ture String 写入的数据,用于写入字符串 data_bytes true Bytes 写入的数据,用于写入字节流 return int 已写入的数据长度,该长度可能小于原数据长度;小于0则表示写错误
list
显示文件列表。
File.list(where)
参数 必选 类型 说明 where true int 要显示的文件位置,见本节常量说明 return Array 文件信息的数组,每个信息是一个对象,有文件名(name)和大小(size)两个属性
getfree
获取外部文件的可用空间,目前只有增强版硬件支持。
File.getfree()
参数 必选 类型 说明 return int 外部文件系统的可用的空间,单位为K字节