ets 怎麼實現文件操作? 關於文件操作的我們可以學習HarmonyOS文件管理和Ability上下文 這兩篇文檔,我這邊實現”文件路徑讀取”、“文件寫入”“文件讀取”,“運行效果”四個方面實現,具體操作如下 1. 文件路徑讀取 參考context.getFilesDir來進行獲取文件路徑,代碼如下 ...
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%')
}
}
效果圖如下: