File 文件处理
创建
creatDirByRoot 创建目录,支持多级创建,root模式
参数名 | 必选 | 类型 | 说明 |
---|---|---|---|
path | 是 | string | 路径 |
laoleng.files.creatDirByRoot("/sdcard/1/")
laoleng.files.creatDirByRoot("/sdcard/1/3/4/")
返回值 | 类型 | 说明 |
---|---|---|
无 | 无 | 无 |
读取
readFileBytes 读取文本bytes数据
参数名 | 必选 | 类型 | 说明 |
---|---|---|---|
filePath | 是 | string | 文件路径 |
logd(laoleng.files.readFileBytes("/sdcard/Pictures/1.zip"));
返回值 | 类型 | 说明 |
---|---|---|
[B@821ea96 | sting | 文本bytes数据 |
readFileBase64 读取文件到Base64数据
参数名 | 必选 | 类型 | 说明 |
---|---|---|---|
fileName | 是 | string | 文件名 |
logd(laoleng.files.readFileBase64("/sdcard/Pictures/1.zip"));
返回值 | 类型 | 说明 |
---|---|---|
YDM | sting | 文件的Base64数据 |
readFileByRoot root模式下读取文件
参数名 | 必选 | 类型 | 说明 |
---|---|---|---|
path | 是 | string | 文件路径 |
logd(laoleng.files.readFileByRoot("/sdcard/1.txt"))
返回值 | 类型 | 说明 |
---|---|---|
文本内容 | String | 文本内容 |
写入
writeFileBytes 写入bytes字节集到文本
参数名 | 必选 | 类型 | 说明 |
---|---|---|---|
fileName | 是 | String | 文件名 |
bytes | 是 | bytes[] | bytes字节流 |
append | 否 | Boolean | 是否追加模式 |
let bytes = "一些字节流"
laoleng.files.writeFileBytes("/sdcard/1.png", bytes)
laoleng.files.writeFileBytes("/sdcard/1.png", bytes, true)
返回值 | 类型 | 说明 |
---|---|---|
true/false | boolean | 是否写入成功 |
writeFileBase64 写入Base64数据到文件
参数名 | 必选 | 类型 | 说明 |
---|---|---|---|
fileName | 是 | string | 文件名 |
base64 | 是 | string | Base64数据 |
let str = laoleng.files.readFileBase64("/sdcard/Pictures/1.zip")
laoleng.files.writeFileBase64("/sdcard/Pictures/2.zip", str)
返回值 | 类型 | 说明 |
---|---|---|
true/false | boolean | 返回结果 |
writeFile 写入文本到文件,可指定编码
参数名 | 必选 | 类型 | 说明 |
---|---|---|---|
fileName | 是 | string | 文件名 |
data | 是 | string | 写入内容 |
encoding | 否 | string | 编码,默认utf-8 |
laoleng.files.writeFile("/sdcard/1.txt", "123")
laoleng.files.writeFile("/sdcard/1.txt", "123", "gbk")
返回值 | 类型 | 说明 |
---|---|---|
true/false | boolean | 是否成功 |
writeFileByRoot root模式下覆盖写入文件
参数名 | 必选 | 类型 | 说明 |
---|---|---|---|
path | 是 | string | 文件路径 |
data | 是 | string | 写入内容 |
laoleng.files.writeFileByRoot("/sdcard/1.txt", "123")
返回值 | 类型 | 说明 |
---|---|---|
无 | 无 | 无 |
writeFileByRoot2 root模式下覆盖写入文件2
参数名 | 必选 | 类型 | 说明 |
---|---|---|---|
path | 是 | string | 文件路径 |
data | 是 | string | 写入内容 |
laoleng.files.writeFileByRoot2("/sdcard/1.txt", "123")
返回值 | 类型 | 说明 |
---|---|---|
无 | 无 | 无 |
writeFileByRoot3 root模式下写入文件,末尾不换行
参数名 | 必选 | 类型 | 说明 |
---|---|---|---|
path | 是 | string | 文件路径 |
data | 是 | string | 写入内容 |
laoleng.files.writeFileByRoot3("/sdcard/1.txt", "123")
返回值 | 类型 | 说明 |
---|---|---|
无 | 无 | 无 |
追加
appendFileByRoot root模式下追加写入文件
参数名 | 必选 | 类型 | 说明 |
---|---|---|---|
path | 是 | string | 文件路径 |
data | 是 | string | 写入内容 |
laoleng.files.appendFileByRoot("/sdcard/1.txt", "123")
返回值 | 类型 | 说明 |
---|---|---|
无 | 无 | 无 |
删除
delByRoot 删除目录或文件,root模式
参数名 | 必选 | 类型 | 说明 |
---|---|---|---|
path | 是 | string | 路径 |
laoleng.files.delByRoot("/sdcard/1.png")
laoleng.files.delByRoot("/sdcard/1/")
返回值 | 类型 | 说明 |
---|---|---|
无 | 无 | 无 |
复制/移动
copyByRoot 复制目录或文件,root模式
参数名 | 必选 | 类型 | 说明 |
---|---|---|---|
src | 是 | string | 起始路径 |
dst | 是 | string | 目标路径 |
laoleng.files.copyByRoot("/sdcard/1.png", "/sdcard/2.png")
laoleng.files.copyByRoot("/sdcard/1/", "/sdcard/2/")
返回值 | 类型 | 说明 |
---|---|---|
无 | 无 | 无 |
moveByRoot 移动目录或文件,root模式
参数名 | 必选 | 类型 | 说明 |
---|---|---|---|
src | 是 | string | 起始路径 |
dst | 是 | string | 目标路径 |
laoleng.files.moveByRoot("/sdcard/1.png", "/sdcard/2.png")
laoleng.files.moveByRoot("/sdcard/1/", "/sdcard/2/")
返回值 | 类型 | 说明 |
---|---|---|
无 | 无 | 无 |
moveOrRenameFile 移动或重命名文件
参数名 | 必选 | 类型 | 说明 |
---|---|---|---|
srcPath | 是 | string | 初始文件名 |
desPath | 是 | string | 目标文件名 |
laoleng.files.moveOrRenameFile("/sdcard/1.png", "/sdcard/2.png")
返回值 | 类型 | 说明 |
---|---|---|
true/false | boolean | 是否成功 |
遍历
dirScanByRoot 遍历文件夹,root模式
参数名 | 必选 | 类型 | 说明 |
---|---|---|---|
path | 是 | string | 目录 路径, 在该目录路径下进行遍历文件或目录 |
name | 否 | string | 文件名或目录名, 可选, 要遍历的文件名或目录名, 支持通配符?, 省略默认为"" |
types | 否 | number | 遍历类型, 可选, 1代表只遍历文件, 2代表只遍历目录, 3代表文件与目录都要遍历, 省略默认为3 |
depths | 否 | number | 遍历深度, 可选, 遍历目录深度, -1表示无限制, 省略默认为-1 |
cases | 否 | boolean | 是否区分大小写, 可选, 省略默认为false, 表示不区分大小写 |
//获取/sdcard/目录下所有文件与目录, 包括子目录
let ret = laoleng.files.dirScanByRoot("/sdcard/", "*")
if (ret) {
for (let i = 0; i < ret.length; i++) {
logd(ret[i])
}
}
//获取/sdcard/目录下所有文件, 包括子目录
ret = laoleng.files.dirScanByRoot("/sdcard/", "*", 1)
if (ret) {
for (let i = 0; i < ret.length; i++) {
logd(ret[i])
}
}
//获取/sdcard/目录下所有目录, 包括子目录
ret = laoleng.files.dirScanByRoot("/sdcard/", "*", 2)
if (ret) {
for (let i = 0; i < ret.length; i++) {
logd(ret[i])
}
}
//获取/sdcard/目录下所有文件和目录, 仅遍历1层
ret = laoleng.files.dirScanByRoot("/sdcard/", "*", 3, 1)
if (ret) {
for (let i = 0; i < ret.length; i++) {
logd(ret[i])
}
}
//遍历/sdcard/目录下以.txt结尾的文件, 仅遍历2层
ret = laoleng.files.dirScanByRoot("/sdcard/", "*.txt", 1, 2)
if (ret) {
for (let i = 0; i < ret.length; i++) {
logd(ret[i])
}
}
返回值 | 类型 | 说明 |
---|---|---|
文件数组 | string[] | 文件数组 |
其他
getLineNumber 获取文件行数
参数名 | 必选 | 类型 | 说明 |
---|---|---|---|
filePath | 是 | string | 文件路径 |
logd(laoleng.files.getLineNumber("/sdcard/文件.txt"));
返回值 | 类型 | 说明 |
---|---|---|
13 | int | 文件行数 |
lastModifyByRoot 获取文件/文件夹最后修改时间,root模式
参数名 | 必选 | 类型 | 说明 |
---|---|---|---|
path | 是 | string | 路径 |
logd(laoleng.files.lastModifyByRoot("/sdcard/1.png"))
logd(laoleng.files.lastModifyByRoot("/sdcard/1/"))
返回值 | 类型 | 说明 |
---|---|---|
1666666666 | number | 时间戳 |
isFileOrDirByRoot 判断是否为文件或者文件夹,root模式
参数名 | 必选 | 类型 | 说明 |
---|---|---|---|
path | 是 | string | 路径 |
//1为文件夹,2为文件,0为路径错误
logd(laoleng.files.isFileOrDirByRoot("/sdcard/1.png"))
logd(laoleng.files.isFileOrDirByRoot("/sdcard/1/"))
返回值 | 类型 | 说明 |
---|---|---|
1 | number | 1为文件夹,2为文件,0为路径错误 |