一.背景
需求是需要操作用户手机中的文件,不是应用沙箱
二.解决方案
这里要注意的一点拿到fsOpen.path的路径再去进行open文件,因为这里还不知道本地文件路径在哪里,需要选择一下路径再拿到路径去请求
1.这里就是进行两个fs.open,拿到你需要操作的文件的路径以后第一个就没用了
const DocumentSelectOptions = new picker.DocumentSelectOptions();
let context = getContext(this) as common.Context;
// 创建文件选择器实例。
const documentViewPicker = new picker.DocumentViewPicker(context);
documentViewPicker.select(DocumentSelectOptions)
.then(async (documentSelectResult) => {
// 获取选择路径
let uri = documentSelectResult[0];
console.log('e2qdw',uri)
// 若文件不存在则创建文件
//const fsOpen = await fs.open(uri,fs.OpenMode.READ_WRITE | fs.OpenMode.READ_ONLY |fs.OpenMode.CREATE)
//console.log(fsOpen.path)
const fsOpen = await fs.open(这里的路径就是fsOpen.path,fs.OpenMode.READ_WRITE | fs.OpenMode.READ_ONLY |fs.OpenMode.CREATE)
// 读取文件
this.readFile(fsOpen)
// 写入
this.writeFile(fsOpen)
})
.catch((err:BusinessError) => {
console.error(`Invoke documentPicker.select failed, code is ${err.code}, message is ${err.message}`);
})
2.第一步拿到以后就可以替换成这个代码 ,找到需要操作文件的路径,DocumentSelectOptions文件选择器就没用了
async getFiles() {
try {
// 若文件不存在则创建文件
const fsOpen = await fs.open('/storage/Users/currentUser/Download/jrdl.json',fs.OpenMode.READ_WRITE | fs.OpenMode.READ_ONLY |fs.OpenMode.CREATE|fs.OpenMode.TRUNC)
// // 读取文件
this.readFile(fsOpen)
// this.deleteFile(fsOpen)
// 写入
this.writeFile(fsOpen)
} catch (e) {
console.error(`Invoke documentViewPicker.save failed, code is ${e.code}, message is ${e.message}`);
}
}
3.读写文件代码
readFile(fsOpen:fs.File){
// 读取文件
let arrayBuffer = new ArrayBuffer(4096);
fs.read(fsOpen.fd, arrayBuffer, (err: BusinessError, readLen: number) => {
if (err) {
console.error("read failed with error message: " + err.message + ", error code: " + err.code);
} else {
console.info("read file data succeed");
let buf = buffer.from(arrayBuffer, 0, readLen);
console.info(`成功读取文件内容: ${buf.toString()}`);
}
// fs.closeSync(fsOpen);
});
}
writeFile(fsOpen:fs.File){
let str: string = "hello,word";
fs.write(fsOpen.fd, str, (err: BusinessError, writeLen: number) => {
if (err) {
console.error("write data to file failed with error message:" + err.message + ", error code: " + err.code);
} else {
promptAction.showToast({ message: '成功写入',duration:2000,alignment:Alignment.Center })
console.info("成功写入大小为:" + writeLen);
}
fs.closeSync(fsOpen);
});
}
4.完整代码
封装成工具类了这里已经,直接拿着就能用
import { BusinessError } from '@kit.BasicServicesKit';
import fs, { ReadOptions, WriteOptions } from '@ohos.file.fs'; // 导入文件管理模块
import { buffer } from '@kit.ArkTS';
import { promptAction } from '@kit.ArkUI';
import { picker } from '@kit.CoreFileKit';
import { common } from '@kit.AbilityKit';
//执行UDP通讯的对象
export class fileUtils {
async getFiles() {
try {
// 若文件不存在则创建文件
const fsOpen = await fs.open('/storage/Users/currentUser/Download/jrdl.json',fs.OpenMode.READ_WRITE | fs.OpenMode.READ_ONLY |fs.OpenMode.CREATE|fs.OpenMode.TRUNC)
// // 读取文件
this.readFile(fsOpen)
// this.deleteFile(fsOpen)
// 写入
this.writeFile(fsOpen)
} catch (e) {
console.error(`Invoke documentViewPicker.save failed, code is ${e.code}, message is ${e.message}`);
}
}
readFile(fsOpen:fs.File){
// 读取文件
let arrayBuffer = new ArrayBuffer(4096);
fs.read(fsOpen.fd, arrayBuffer, (err: BusinessError, readLen: number) => {
if (err) {
console.error("read failed with error message: " + err.message + ", error code: " + err.code);
} else {
console.info("read file data succeed");
let buf = buffer.from(arrayBuffer, 0, readLen);
console.info(`成功读取文件内容: ${buf.toString()}`);
}
// fs.closeSync(fsOpen);
});
}
writeFile(fsOpen:fs.File){
let str: string = "hello,word";
fs.write(fsOpen.fd, str, (err: BusinessError, writeLen: number) => {
if (err) {
console.error("write data to file failed with error message:" + err.message + ", error code: " + err.code);
} else {
promptAction.showToast({ message: '成功写入',duration:2000,alignment:Alignment.Center })
console.info("成功写入大小为:" + writeLen);
}
fs.closeSync(fsOpen);
});
}
deleteFile(){
fs.unlink('/storage/Users/currentUser/Download/jrdl.json').then(() => {
console.info("remove file succeed");
}).catch((err: BusinessError) => {
console.error("remove file failed with error message: " + err.message + ", error code: " + err.code);
});
}
}
5.调用
new fileUtils().getFiles()