HarmonyOS鸿蒙Next中怎么将一个在沙箱存入一个数据,然后再读取沙箱中这个json文件的数据,输出文件结果
HarmonyOS鸿蒙Next中怎么将一个在沙箱存入一个数据,然后再读取沙箱中这个json文件的数据,输出文件结果 怎么将一个在沙箱存入一个数据,然后再读取沙箱中这个json文件的数据,输出文件结果
3 回复
参考如下demo:
import fs from '@ohos.file.fs';
import { buffer } from '@kit.ArkTS';
export class loginInfo{
server:string = ''
username:string = ''
password:string = ''
}
export class Config{
loginInfo:loginInfo = new loginInfo()
autoLogin:boolean = false
jumpTime:number = 0
}
export function createConfig(): void {
let config: Config = {
loginInfo: {
server: '168.192.1.1',
username: 'admin',
password: 'admin123'
},
autoLogin: false,
jumpTime: 10
}
// fs.unlinkSync(filesDir + '/config.json')
let configStatus = fs.accessSync(filesDir + '/config.json')
if (configStatus === true) {
console.log("配置文件存在。")
} else {
console.log("配置文件不存在。")
let file = fs.openSync(filesDir + '/config.json', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE | fs.OpenMode.TRUNC);
fs.writeSync(file.fd, JSON.stringify(config));
fs.closeSync(file);
}
console.log("配置文件位置", filesDir + '/config.json')
}
export function readConfig(): Config {
let config: Config
let file = fs.openSync(filesDir + '/config.json', fs.OpenMode.READ_WRITE)
let arrayBuffer = new ArrayBuffer(4096);
let readLen = fs.readSync(file.fd, arrayBuffer);
let buf = buffer.from(arrayBuffer, 0, readLen);
config = JSON.parse(buf.toString())
console.log("读取配置", JSON.stringify(config))
fs.closeSync(file);
return config
}
const filesDir = getContext(this).filesDir
@Entry
@Component
struct FsFilePage {
@State message: string = 'Hello World';
aboutToAppear(): void {
createConfig()
}
build() {
RelativeContainer() {
Text(this.message)
.id('FsFilePageHelloWorld')
.fontSize(16)
.fontWeight(FontWeight.Normal)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
.onClick(()=>{
let config = readConfig()
this.message = JSON.stringify(config)
})
}
.height('100%')
.width('100%')
}
}
更多关于HarmonyOS鸿蒙Next中怎么将一个在沙箱存入一个数据,然后再读取沙箱中这个json文件的数据,输出文件结果的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,可以通过File
和FileReader
类来实现将数据存入沙箱,并读取沙箱中的JSON文件数据。以下是一个简单的示例代码,展示如何实现这一功能:
- 将数据存入沙箱:
import fileio from '@ohos.fileio';
// 定义要存储的JSON数据
let jsonData = {
name: "HarmonyOS",
version: "Next"
};
// 将JSON对象转换为字符串
let jsonString = JSON.stringify(jsonData);
// 定义文件路径
let filePath = '/data/storage/el2/base/haps/test.json';
// 将数据写入文件
fileio.writeFileSync(filePath, jsonString, { encoding: 'utf-8' });
- 读取沙箱中的JSON文件数据:
import fileio from '@ohos.fileio';
// 定义文件路径
let filePath = '/data/storage/el2/base/haps/test.json';
// 读取文件内容
let fileContent = fileio.readFileSync(filePath, { encoding: 'utf-8' });
// 将字符串转换为JSON对象
let jsonData = JSON.parse(fileContent);
// 输出JSON数据
console.log(jsonData);
- 输出文件结果:
import fileio from '@ohos.fileio';
// 定义文件路径
let filePath = '/data/storage/el2/base/haps/test.json';
// 读取文件内容
let fileContent = fileio.readFileSync(filePath, { encoding: 'utf-8' });
// 将字符串转换为JSON对象
let jsonData = JSON.parse(fileContent);
// 输出JSON数据
console.log(jsonData);
通过以上代码,你可以在HarmonyOS鸿蒙Next中实现将JSON数据存入沙箱,并读取沙箱中的JSON文件数据,最后输出文件结果。
在HarmonyOS鸿蒙Next中,可以通过以下步骤将JSON数据存入沙箱并读取:
-
写入JSON文件:
const context = getContext(this); const filePath = context.filesDir + "/data.json"; const jsonData = { name: "HarmonyOS", version: "Next" }; const file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.WRITE_ONLY); fs.writeSync(file.fd, JSON.stringify(jsonData)); fs.closeSync(file.fd);
-
读取JSON文件:
const file = fs.openSync(filePath, fs.OpenMode.READ_ONLY); const content = fs.readSync(file.fd); fs.closeSync(file.fd); const jsonResult = JSON.parse(content); console.log(jsonResult);
这样即可实现将JSON数据存入沙箱并读取输出的功能。