Skip to main content

文件 JavaScriptApi

文件相关接口,JavaScript 对应对象为 window.NativeFile

写内容到文件

boolean writeFile(String filePath, String content, boolean append)

写内容到文件

参数

ParamDescription
filePath文件绝对路径,如果不存在则自动创建,建议使用/sdcard/shine/web 作为根目录
content写入内容
append是否追加写入

返回值

是否写入成功

调用示例
window.NativeFile.writeFile("/sdcard/shine/web/test.txt", "hello world", false);

读文件内容

String readFile(String filePath)

读文件内容

参数

ParamDescription
filePath文件绝对路径

返回值

文件内容

调用示例
window.NativeFile.readFile("/sdcard/shine/web/test.txt");

读文件大小

long readFileLength(String filePath)

读文件大小

参数

ParamDescription
filePath文件绝对路径

返回值

文件大小,单位字节

调用示例
window.NativeFile.readFileLength("/sdcard/shine/web/test.txt");

获取文件最后修改时间

long readFileLastModified(String filePath)

获取文件最后修改时间

参数

ParamDescription
filePath文件绝对路径

返回值

文件最后修改时间

调用示例
window.NativeFile.readFileLastModified("/sdcard/shine/web/test.txt");

删除文件

boolean deleteFile(String filePath)

删除文件

参数

ParamDescription
filePath文件绝对路径

返回值

是否删除成功

调用示例
window.NativeFile.deleteFile("/sdcard/shine/web/test.txt");

检测文件是否存在

boolean isFileExists(String filePath)

检测文件是否存在

参数

ParamDescription
filePath文件绝对路径

返回值

文件是否存在

调用示例
window.NativeFile.isFileExists("/sdcard/shine/web/test.txt");

压缩文件

void zipFile(String filePath, String destPath, SimpleCallBack callBack)

压缩文件

参数

ParamDescription
filePath文件绝对路径
destPath压缩包绝对路径
callBack结果回调
调用示例
window.NativeFile.zipFile("/sdcard/log.txt", "/sdcard/log.zip", function (result) {
// result: {"code":0,"data":"/sdcard/log.zip"}
// code: 0-成功,其他-失败,data: 压缩包绝对路径
var data = JSON.parse(result);
if (data.code === 0) {
console.log("zip success");
} else {
var errorMsg = data.data;
console.log("zip fail: " + errorMsg);
}
});

解压压缩包

void unZipFile(String filePath, String destPath, SimpleCallBack callBack)

解压压缩包

参数

ParamDescription
filePath压缩包绝对路径
destPath解压文件夹绝对路径
callBack结果回调
调用示例
window.NativeFile.unZipFile("/sdcard/log.zip", "/sdcard/log/", function (result) {
// result: {"code":0,"data": ["/sdcard/log1.txt", "/sdcard/log2.txt"]}
// code: 0-成功,其他-失败,data: 解压后所有文件绝对路径列表
var data = JSON.parse(result);
if (data.code === 0) {
console.log("unZipFile success");
} else {
var errorMsg = data.data;
console.log("unZipFile fail: " + errorMsg);
}
});

base64编码

String encode2Base64(String filePath)

文件转base64格式数据,采用编码标志位为NO_WRAP

参数

ParamDescription
filePath文件绝对路径
调用示例
window.NativeFile.encode2Base64("/sdcard/test.png");