ets 怎么實作檔案操作?
關于檔案操作的我們可以學習HarmonyOS檔案管理和Ability背景關系 這兩篇檔案,我這邊實作”檔案路徑讀取”、“檔案寫入”“檔案讀取”,“運行效果”四個方面實作,具體操作如下
1. 檔案路徑讀取
參考context.getFilesDir來進行獲取檔案路徑,代碼如下

private getCacheDir() {
var context = ability_featureAbility.getContext();
context.getFilesDir()
.then((data) => {
console.log('File directory obtained. Data:' + data);
this.path=data;
}).catch((error) => {
console.error('Failed to obtain the file directory. Cause: ' + error.message);
})
}
2. 檔案寫入
參考fileio.openSync的api,實作代碼如下

private writeFiles(){
let fd = fileio.openSync(this.path+"/111.txt", 0o102, 0o666);
fileio.write(fd, "你好 2022", function (err, bytesWritten) {
if (!err) {
console.log("寫入成功");
}
});
}
3. 檔案讀取
參考 fileio.read這個api ,代碼如下

private ReadFile() {
let Filepath = this.path+"/111.txt";
let fd = fileio.openSync(Filepath, 0o2);
let buf = new ArrayBuffer(4096);
fileio.read(fd, buf, function (err, readOut) {
if (!err) {
let encodedString = String.fromCodePoint.apply(null, new Uint8Array(readOut.buffer));
let decodedString = decodeURIComponent(escape(encodedString));//沒有這一步中文會亂碼
console.log("讀取檔案內容"+decodedString);
}
});
}
4. 運行效果
全部代碼如下
import fileio from '@ohos.fileio';
import ability_featureAbility from '@ohos.ability.featureAbility';
@Entry
@Component
struct NewmyFileTwo {
@State path:string="";
private getCacheDir(){
var context = ability_featureAbility.getContext();
context.getFilesDir()
.then((data) => {
console.log('File directory obtained. Data:' + data);
this.path=data;
}).catch((error) => {
console.error('Failed to obtain the file directory. Cause: ' + error.message);
})
}
private writeFiles(){
let fd = fileio.openSync(this.path+"/111.txt", 0o102, 0o666);
fileio.write(fd, "你好 2022", function (err, bytesWritten) {
if (!err) {
console.log("寫入成功")
}
});
}
private ReadFile(){
let Filepath = this.path+"/111.txt";
let fd = fileio.openSync(Filepath, 0o2);
let buf = new ArrayBuffer(4096);
fileio.read(fd, buf, function (err, readOut) {
if (!err) {
let encodedString = String.fromCodePoint.apply(null, new Uint8Array(readOut.buffer));
let decodedString = decodeURIComponent(escape(encodedString));//沒有這一步中文會亂碼
console.log("讀取檔案內容"+decodedString);
}
});
}
private getFilesDirNew(){
var context = ability_featureAbility.getContext();
context.getFilesDir()
.then((data) => {
console.log('File directory obtained. Data:' + data);
this.path=data;
}).catch((error) => {
console.error('Failed to obtain the file directory. Cause: ' + error.message);
})
}
build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Text('獲取檔案目錄')
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(this.getFilesDirNew.bind(this));
Text('寫檔案 你好 2022 到檔案中')
.fontSize(50)
.fontWeight(FontWeight.Bold)
.backgroundColor(Color.Red)
.onClick(this.writeFiles.bind(this));
Text('讀檔案內容')
.fontSize(50)
.fontWeight(FontWeight.Bold)
.backgroundColor(Color.White)
.onClick(this.ReadFile.bind(this));
}
.width('100%')
.height('100%')
}
}
效果圖如下:

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/449127.html
標籤:其他
