参照文档,进行文件写入和读取时,出现读取不到或闪退
export function createFile() {
// 获取应用文件路径
let context = getContext(this) as common.UIAbilityContext;
let filesDir = context.filesDir;
// 新建并打开文件
let file = fs.openSync(filesDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
// 写入一段内容至文件
let content = "static
存储类指示编译器在程序的生命周期内保持局部变量的存在,而不需要在每次它进入和离开作用域时进行创建和销毁,因此使用static修饰局部变量可以在函数调用之间保持局部变量的值register 用于定义存储在寄存器中而不是RAM中的局部变量,不过需要注意,定义 ‘register’ 并不意味着变量将被存储在寄存器中,它意味着变量可能存储在寄存器中,这取决于硬件和实现的限制。";
let writeLen = fs.writeSync(file.fd, content);
console.info("The length of str is: " + writeLen);
// 从文件读取一段内容
let buf = new ArrayBuffer(4096);
let readLen = fs.readSync(file.fd, buf, { offset: 0 });
let s = String.fromCharCode.apply(null, new Uint8Array(buf.slice(0, readLen)));
let d = decodeURIComponent(escape(s));
console.info("the content of file: " + d);
// 关闭文件
fs.closeSync(file);
}
上面这段代码是直接拷贝文档的,就修改了下写入内容,在执行到 let d = decodeURIComponent(escape(s)); 程序直接闪退,每次都是。然后自己将文件操作方法写成了工具类
export function getFilesDir(): string {
// 获取应用文件路径
let context = getContext(this) as common.UIAbilityContext;
return context.filesDir;
}
//<路径前缀>/<加密等级>/base/files/
//表示 APP 内部存储的缓存目录,可读写,但是随时可能被清楚,不保证持久性,一般用作下载临时目录或者缓存目录。
export function getCacheDir(): string {
let context = getContext(this) as common.UIAbilityContext;
return context.cacheDir;
}
//将内容写入内存中
export function writeFile(fileName: string, content: string) {
// 新建并打开文件
let file = fs.openSync(getFilesDir() + "/" + fileName, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
// 写入一段内容至文件
let writeLen = fs.writeSync(file.fd, content);
console.info("The length of str is: " + writeLen);
// 关闭文件
fs.closeSync(file);
}
//从文件中读取内容
export function readFile(fileName: string) {
// 打开文件
let srcFile = fs.openSync(getFilesDir() + "/" + fileName, fs.OpenMode.READ_WRITE);
// 读取源文件内容并写入至目的文件
let bufSize = 4096;
let readSize = 0;
let buf = new ArrayBuffer(bufSize);
let readLen = fs.readSync(srcFile.fd, buf, { offset: readSize });
while (readLen > 0) {
readSize += readLen;
readLen = fs.readSync(srcFile.fd, buf, { offset: readSize });
}
let s = String.fromCodePoint.apply(null, new Uint8Array(buf));
console.log("readFile=============s==" + s);
let d = decodeURIComponent(escape(s));
console.log("readFile=============d==" + d);
// 关闭文件
fs.closeSync(srcFile);
}
//以流的形式读取文件
export async function readIOFile(fileName: string) {
// 打开文件流
let inputStream = fs.createStreamSync(getFilesDir() + '/' + fileName, 'r+');
// 以流的形式读取源文件内容并写入目的文件
let bufSize = 4096;
let readSize = 0;
let buf = new ArrayBuffer(bufSize);
let readLen = await inputStream.read(buf, { offset: readSize });
readSize += readLen;
while (readLen > 0) {
readLen = await inputStream.read(buf, { offset: readSize });
readSize += readLen;
}
let s = String.fromCodePoint.apply(null, new Uint8Array(buf));
console.log("readFile=============s==" + s);
let d = decodeURIComponent(escape(s));
console.log("readFile=============d==" + d);
// 关闭文件流
inputStream.closeSync();
}
文件读写可参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides...