From 59251056265f30ff183bae75f31145b146419304 Mon Sep 17 00:00:00 2001 From: kuangning <2540487435@qq.com> Date: Thu, 15 May 2025 18:40:43 +0800 Subject: [PATCH 1/2] =?UTF-8?q?CoreFileKit=E6=B7=BB=E5=8A=A0=E6=A0=87?= =?UTF-8?q?=E8=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entry/src/main/ets/pages/CacheSizeAndCleanupAPI.ets | 4 ++-- CoreFileKit/entry/src/main/ets/pages/CheckStorageSpace.ets | 4 ++-- CoreFileKit/entry/src/main/ets/pages/CreateTempFile.ets | 4 ++-- CoreFileKit/entry/src/main/ets/pages/FileContentParser.ets | 4 ++-- CoreFileKit/entry/src/main/ets/pages/GetRawfile.ets | 4 ++-- .../entry/src/main/ets/pages/HandleFileEncodingProblem.ets | 4 ++-- CoreFileKit/entry/src/main/ets/pages/StreamDataToFile.ets | 4 ++-- CoreFileKit/entry/src/main/ets/pages/UpdateSandboxJson.ets | 4 ++-- 8 files changed, 16 insertions(+), 16 deletions(-) diff --git a/CoreFileKit/entry/src/main/ets/pages/CacheSizeAndCleanupAPI.ets b/CoreFileKit/entry/src/main/ets/pages/CacheSizeAndCleanupAPI.ets index c845ac4..4a6641b 100644 --- a/CoreFileKit/entry/src/main/ets/pages/CacheSizeAndCleanupAPI.ets +++ b/CoreFileKit/entry/src/main/ets/pages/CacheSizeAndCleanupAPI.ets @@ -17,7 +17,7 @@ * FAQ:怎么获取应用已使用的缓存大小,如何使用API清理缓存 */ -// DocsCode 1 +// [Start CacheSizeAndCleanupAPI] import { fileIo, storageStatistics } from '@kit.CoreFileKit'; import { BusinessError } from '@kit.BasicServicesKit'; @@ -94,4 +94,4 @@ struct ClearCache { } } } -// DocsCode 1 \ No newline at end of file +// [End CacheSizeAndCleanupAPI] \ No newline at end of file diff --git a/CoreFileKit/entry/src/main/ets/pages/CheckStorageSpace.ets b/CoreFileKit/entry/src/main/ets/pages/CheckStorageSpace.ets index e8ddc26..0187623 100644 --- a/CoreFileKit/entry/src/main/ets/pages/CheckStorageSpace.ets +++ b/CoreFileKit/entry/src/main/ets/pages/CheckStorageSpace.ets @@ -17,7 +17,7 @@ * FAQ:获取指定文件系统的剩余空间大小 */ -// DocsCode 1 +// [Start CheckStorageSpace] import { BusinessError } from '@kit.BasicServicesKit'; import { statfs } from '@kit.CoreFileKit'; @@ -48,4 +48,4 @@ struct GetFileSpace { } } } -// DocsCode 1 \ No newline at end of file +// [End CheckStorageSpace] \ No newline at end of file diff --git a/CoreFileKit/entry/src/main/ets/pages/CreateTempFile.ets b/CoreFileKit/entry/src/main/ets/pages/CreateTempFile.ets index f1f0522..e5d6c05 100644 --- a/CoreFileKit/entry/src/main/ets/pages/CreateTempFile.ets +++ b/CoreFileKit/entry/src/main/ets/pages/CreateTempFile.ets @@ -17,7 +17,7 @@ * FAQ:如何创建临时文件 */ -// DocsCode 1 +// [Start CreateTempFile] import { fileIo as fs, ReadOptions } from '@kit.CoreFileKit'; import { common } from '@kit.AbilityKit'; import { buffer } from '@kit.ArkTS'; @@ -66,4 +66,4 @@ struct CreateFileDemo { .width('100%') } } -// DocsCode 1 \ No newline at end of file +// [End CreateTempFile] \ No newline at end of file diff --git a/CoreFileKit/entry/src/main/ets/pages/FileContentParser.ets b/CoreFileKit/entry/src/main/ets/pages/FileContentParser.ets index 47c11a8..83301de 100644 --- a/CoreFileKit/entry/src/main/ets/pages/FileContentParser.ets +++ b/CoreFileKit/entry/src/main/ets/pages/FileContentParser.ets @@ -17,7 +17,7 @@ * FAQ:如何读取指定文件内容,并转为具体对象 */ -// DocsCode 1 +// [Start FileContentParser] import { Context } from '@kit.AbilityKit'; import { buffer } from '@kit.ArkTS'; @@ -59,4 +59,4 @@ struct Index { }.width('100%') } } -// DocsCode 1 \ No newline at end of file +// [End FileContentParser] \ No newline at end of file diff --git a/CoreFileKit/entry/src/main/ets/pages/GetRawfile.ets b/CoreFileKit/entry/src/main/ets/pages/GetRawfile.ets index c826f04..896814d 100644 --- a/CoreFileKit/entry/src/main/ets/pages/GetRawfile.ets +++ b/CoreFileKit/entry/src/main/ets/pages/GetRawfile.ets @@ -17,7 +17,7 @@ * FAQ:如何获取到 resources下rawfile 的文件 */ -// DocsCode 1 +// [Start GetRawfile] import { fileIo } from '@kit.CoreFileKit'; @Component @@ -52,4 +52,4 @@ export struct GetRawfile { .width('100%') } } -// DocsCode 1 \ No newline at end of file +// [End GetRawfile] \ No newline at end of file diff --git a/CoreFileKit/entry/src/main/ets/pages/HandleFileEncodingProblem.ets b/CoreFileKit/entry/src/main/ets/pages/HandleFileEncodingProblem.ets index 7a09f94..073b9af 100644 --- a/CoreFileKit/entry/src/main/ets/pages/HandleFileEncodingProblem.ets +++ b/CoreFileKit/entry/src/main/ets/pages/HandleFileEncodingProblem.ets @@ -17,7 +17,7 @@ * FAQ:如何解决文件的中文乱码问题 */ -// DocsCode 1 +// [Start HandleFileEncodingProblem] import { util } from '@kit.ArkTS'; import { fileIo } from '@kit.CoreFileKit'; @@ -38,4 +38,4 @@ let textDecoder = util.TextDecoder.create('utf-8', { ignoreBOM: true }); // 解码输后获取对应文本 let readString = textDecoder.decodeToString(new Uint8Array(buffer), { stream: false }); console.info(`GarbledCnCharacters read content is:${readString}`); -// DocsCode 1 \ No newline at end of file +// [End HandleFileEncodingProblem] \ No newline at end of file diff --git a/CoreFileKit/entry/src/main/ets/pages/StreamDataToFile.ets b/CoreFileKit/entry/src/main/ets/pages/StreamDataToFile.ets index 1ffa416..c1f1d4d 100644 --- a/CoreFileKit/entry/src/main/ets/pages/StreamDataToFile.ets +++ b/CoreFileKit/entry/src/main/ets/pages/StreamDataToFile.ets @@ -17,7 +17,7 @@ * FAQ:如何将数据持续写入文件内 */ -// DocsCode 1 +// [Start StreamDataToFile] import { fileIo, WriteOptions } from '@kit.CoreFileKit'; import { BusinessError } from '@kit.BasicServicesKit'; @@ -58,4 +58,4 @@ export struct ContinuousWrite { .width('100%') } } -// DocsCode 1 \ No newline at end of file +// [End StreamDataToFile] \ No newline at end of file diff --git a/CoreFileKit/entry/src/main/ets/pages/UpdateSandboxJson.ets b/CoreFileKit/entry/src/main/ets/pages/UpdateSandboxJson.ets index b4b4207..a88de89 100644 --- a/CoreFileKit/entry/src/main/ets/pages/UpdateSandboxJson.ets +++ b/CoreFileKit/entry/src/main/ets/pages/UpdateSandboxJson.ets @@ -17,7 +17,7 @@ * FAQ:如何修改沙箱路径下json文件的指定内容 */ -// DocsCode 1 +// [Start UpdateSandboxJson] import { fileIo } from '@kit.CoreFileKit'; // 工具类中:在EntryAbility中获取Context后保存至AppStorage,然后在工具类中使用AppStorage获取 @@ -46,4 +46,4 @@ fileIo.close(fileModify); // 5 读取最新内容 let content = fileIo.readTextSync(filePath); console.info(`ModifySanFileContent content is :${content}`); -// DocsCode 1 \ No newline at end of file +// [End UpdateSandboxJson] \ No newline at end of file -- Gitee From 88d7c325d8b2c0feb779766e5a0e55abf9b61124 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E9=93=83?= <2540487435@qq.com> Date: Sat, 17 May 2025 06:39:22 +0000 Subject: [PATCH 2/2] =?UTF-8?q?CoreFileKit=E4=BF=AE=E6=94=B9=E6=B3=A8?= =?UTF-8?q?=E9=87=8A=E4=B8=BA=E8=8B=B1=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 风铃 <2540487435@qq.com> --- .../src/main/ets/pages/CacheSizeAndCleanupAPI.ets | 14 +++++++------- .../entry/src/main/ets/pages/CheckStorageSpace.ets | 6 +++--- .../entry/src/main/ets/pages/CreateTempFile.ets | 8 ++++---- .../entry/src/main/ets/pages/FileContentParser.ets | 6 +++--- .../entry/src/main/ets/pages/GetRawfile.ets | 2 +- .../main/ets/pages/HandleFileEncodingProblem.ets | 12 ++++++------ .../entry/src/main/ets/pages/StreamDataToFile.ets | 2 +- .../entry/src/main/ets/pages/UpdateSandboxJson.ets | 12 ++++++------ 8 files changed, 31 insertions(+), 31 deletions(-) diff --git a/CoreFileKit/entry/src/main/ets/pages/CacheSizeAndCleanupAPI.ets b/CoreFileKit/entry/src/main/ets/pages/CacheSizeAndCleanupAPI.ets index 4a6641b..72a7c43 100644 --- a/CoreFileKit/entry/src/main/ets/pages/CacheSizeAndCleanupAPI.ets +++ b/CoreFileKit/entry/src/main/ets/pages/CacheSizeAndCleanupAPI.ets @@ -24,7 +24,7 @@ import { BusinessError } from '@kit.BasicServicesKit'; @Entry @Component struct ClearCache { - // 在缓存中创建一个文件 + // Create a file in the cache writeFile() { let filePath = this.getUIContext().getHostContext()!.cacheDir + '/test.txt'; let fileStream = fileIo.createStreamSync(filePath, 'w+'); @@ -32,7 +32,7 @@ struct ClearCache { fileStream.close(); } - // 获取应用数据空间大小 + // Obtain the size of the application data space getCache() { storageStatistics.getCurrentBundleStats((error: BusinessError, bundleStats: storageStatistics.BundleStats) => { if (error) { @@ -46,7 +46,7 @@ struct ClearCache { }); } - // 清理缓存 + // Clear cache clearCache() { let cacheDir = this.getUIContext().getHostContext()!.cacheDir; console.info(cacheDir); @@ -55,7 +55,7 @@ struct ClearCache { for (let i = 0; i < filenames.length; i++) { let dirPath = cacheDir + '/' + filenames[i]; console.log(dirPath); - // 判断是否为文件夹 + // Determine whether it is a folder let isDirectory: boolean = false; try { isDirectory = fileIo.statSync(dirPath).isDirectory(); @@ -79,15 +79,15 @@ struct ClearCache { build() { Column() { - Button('向缓存写入数据') + Button('Write data to cache') .onClick(() => { this.writeFile(); }) - Button('获取系统缓存大小') + Button('Get the system cache size') .onClick(() => { this.getCache(); }) - Button('点击清理缓存') + Button('Click to clear cache') .onClick(() => { this.clearCache(); }) diff --git a/CoreFileKit/entry/src/main/ets/pages/CheckStorageSpace.ets b/CoreFileKit/entry/src/main/ets/pages/CheckStorageSpace.ets index 0187623..c0d928a 100644 --- a/CoreFileKit/entry/src/main/ets/pages/CheckStorageSpace.ets +++ b/CoreFileKit/entry/src/main/ets/pages/CheckStorageSpace.ets @@ -26,19 +26,19 @@ import { statfs } from '@kit.CoreFileKit'; struct GetFileSpace { build() { Column() { - Button('获取系统空间大小') + Button('Obtain the system space size') .onClick(() => { let context = this.getUIContext().getHostContext(); let path = context!.filesDir; - //获取指定文件系统总字节数 + //Get the total number of bytes in the specified file system statfs.getTotalSize(path).then((number: number) => { console.info('getTotalSize succeed, Size: ' + number); }).catch((err: BusinessError) => { console.error('getTotalSize failed with error message: ' + err.message + ', error code: ' + err.code); }); - //获取指定文件系统空闲字节数 + //Get the number of idle bytes in the specified file system statfs.getFreeSize(path).then((number: number) => { console.info('getFreeSize succeed, Size: ' + number); }).catch((err: BusinessError) => { diff --git a/CoreFileKit/entry/src/main/ets/pages/CreateTempFile.ets b/CoreFileKit/entry/src/main/ets/pages/CreateTempFile.ets index e5d6c05..0669c1f 100644 --- a/CoreFileKit/entry/src/main/ets/pages/CreateTempFile.ets +++ b/CoreFileKit/entry/src/main/ets/pages/CreateTempFile.ets @@ -38,10 +38,10 @@ struct CreateFileDemo { let context = this.getUIContext().getHostContext(); let filesDir = context!.tempDir; let file = fs.openSync(filesDir + 'test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); - // 写入一段内容至文件 + // Write a paragraph of content to a file fs.writeSync(file.fd, 'Try to write str.'); console.info('str has been written'); - // 关闭文件 + // close file fs.closeSync(file); }) Button(this.readStr) @@ -49,7 +49,7 @@ struct CreateFileDemo { let context = this.getUIContext().getHostContext(); let filesDir = context!.tempDir; let file = fs.openSync(filesDir + 'test.txt', fs.OpenMode.READ_WRITE); - // 从文件读取一段内容 + // Read a section of content from a file let arrayBuffer = new ArrayBuffer(1024); let readOptions: ReadOptions = { offset: 0, @@ -58,7 +58,7 @@ struct CreateFileDemo { let readLen = fs.readSync(file.fd, arrayBuffer, readOptions); let buf = buffer.from(arrayBuffer, 0, readLen); this.message = buf.toString(); - // 关闭文件 + // close file fs.closeSync(file); }) } diff --git a/CoreFileKit/entry/src/main/ets/pages/FileContentParser.ets b/CoreFileKit/entry/src/main/ets/pages/FileContentParser.ets index 83301de..8621c02 100644 --- a/CoreFileKit/entry/src/main/ets/pages/FileContentParser.ets +++ b/CoreFileKit/entry/src/main/ets/pages/FileContentParser.ets @@ -28,7 +28,7 @@ struct Index { private str: string = ''; getRawFile(): ESObject { - //调用getRawFileContent接口获取json文件内容,并读为string + //Call the getRawFileContent interface to retrieve the content of a JSON file and read it as a string this.getUIContext().getHostContext()!.resourceManager.getRawFileContent('test.json', (err, data) => { try { this.str = buffer.from(data.buffer).toString(); @@ -37,14 +37,14 @@ struct Index { console.info(JSON.stringify(e)); } }) - //也可以调用getRawFileContentSync接口获取json文件内容,并读为string + //You can also call the getRawFileContentSync interface to retrieve the content of the JSON file and read it as a string try { let data: Uint8Array = this.context!.resourceManager.getRawFileContentSync('test.json'); this.str = buffer.from(data.buffer).toString(); } catch (e) { console.info(JSON.stringify(e)); } - // string转为ESObject + // Convert string to ESObject let obj: ESObject = JSON.parse(this.str); console.info('ESObject', JSON.stringify(obj)); return obj; diff --git a/CoreFileKit/entry/src/main/ets/pages/GetRawfile.ets b/CoreFileKit/entry/src/main/ets/pages/GetRawfile.ets index 896814d..ecbd2a9 100644 --- a/CoreFileKit/entry/src/main/ets/pages/GetRawfile.ets +++ b/CoreFileKit/entry/src/main/ets/pages/GetRawfile.ets @@ -27,7 +27,7 @@ export struct GetRawfile { aboutToAppear(): void { getContext(this).resourceManager.getRawFileContent('test.txt', (_err, value) => { let myBuffer: ArrayBufferLike = value.buffer; - let context = this.getUIContext().getHostContext(); //沙箱路径 + let context = this.getUIContext().getHostContext(); //Sandbox Path let filePath = context!.filesDir + '/test.txt'; console.info('testTag-filePath:' + filePath); let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); diff --git a/CoreFileKit/entry/src/main/ets/pages/HandleFileEncodingProblem.ets b/CoreFileKit/entry/src/main/ets/pages/HandleFileEncodingProblem.ets index 073b9af..c2f9440 100644 --- a/CoreFileKit/entry/src/main/ets/pages/HandleFileEncodingProblem.ets +++ b/CoreFileKit/entry/src/main/ets/pages/HandleFileEncodingProblem.ets @@ -21,21 +21,21 @@ import { util } from '@kit.ArkTS'; import { fileIo } from '@kit.CoreFileKit'; -// 工具类中:在EntryAbility中获取Context后保存至AppStorage,然后在工具类中使用AppStorage获取 +// In the utility class, retrieve the Context from the Entry Ability and save it to AppStore, then use AppStore to retrieve it in the utility class let context = AppStorage.get("context") as UIContext; -// 创建一个文件写入中文字符 +// Create a file to write Chinese characters let filePath = context.getHostContext()!.filesDir + "/test0.txt"; let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); -// 写入一段内容至文件 -let writeLen = fileIo.writeSync(file.fd, "你好,世界"); +// Write a paragraph of content to a file +let writeLen = fileIo.writeSync(file.fd, "Hello, world"); fileIo.closeSync(file); console.info(`GarbledCnCharacters The length of str is: ${writeLen}`); let stream = fileIo.createStreamSync(filePath, "r+"); let buffer = new ArrayBuffer(4096); stream.readSync(buffer); -// 设置编码格式为“utf-8” +// Set the encoding format to "utf-8" let textDecoder = util.TextDecoder.create('utf-8', { ignoreBOM: true }); -// 解码输后获取对应文本 +// Retrieve the corresponding text after decoding the input let readString = textDecoder.decodeToString(new Uint8Array(buffer), { stream: false }); console.info(`GarbledCnCharacters read content is:${readString}`); // [End HandleFileEncodingProblem] \ No newline at end of file diff --git a/CoreFileKit/entry/src/main/ets/pages/StreamDataToFile.ets b/CoreFileKit/entry/src/main/ets/pages/StreamDataToFile.ets index c1f1d4d..087b52e 100644 --- a/CoreFileKit/entry/src/main/ets/pages/StreamDataToFile.ets +++ b/CoreFileKit/entry/src/main/ets/pages/StreamDataToFile.ets @@ -26,7 +26,7 @@ export struct ContinuousWrite { @State message: string = 'Hello World'; aboutToAppear(): void { - let context = this.getUIContext().getHostContext(); // 沙箱路径 + let context = this.getUIContext().getHostContext(); // Sandbox Path let filePath = context!.filesDir + '/test.txt'; let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE); let arrayBuffer = new ArrayBuffer(4096); diff --git a/CoreFileKit/entry/src/main/ets/pages/UpdateSandboxJson.ets b/CoreFileKit/entry/src/main/ets/pages/UpdateSandboxJson.ets index a88de89..cb8e2b5 100644 --- a/CoreFileKit/entry/src/main/ets/pages/UpdateSandboxJson.ets +++ b/CoreFileKit/entry/src/main/ets/pages/UpdateSandboxJson.ets @@ -20,7 +20,7 @@ // [Start UpdateSandboxJson] import { fileIo } from '@kit.CoreFileKit'; -// 工具类中:在EntryAbility中获取Context后保存至AppStorage,然后在工具类中使用AppStorage获取 +// In the utility class, retrieve the Context from the Entry Ability and save it to AppStore, then use AppStore to retrieve it in the utility class let context = AppStorage.get("context") as UIContext; let filePath = context.getHostContext()!.filesDir + '/people.json'; @@ -30,20 +30,20 @@ class Student { } let student = new Student(); -// 1 创建文件,并且写入内容 +// 1 Create a file and write its contents let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); fileIo.writeSync(file.fd, JSON.stringify(student)) fileIo.close(file); -// 2 通过fileIo.readSync读取json文件内容。 +// 2 Read the contents of the JSON file through fileIo.readSync. let data = fileIo.readTextSync(filePath); let obj: Student = JSON.parse(data); -// 3 修改指定内容name为lisi +// 3 Change the specified content name to lisi obj.name = 'lisi'; -// 4 重新写入json文件 +// 4 Rewrite JSON file let fileModify = fileIo.openSync(filePath, fileIo.OpenMode.WRITE_ONLY | fileIo.OpenMode.TRUNC); fileIo.writeSync(fileModify.fd, JSON.stringify(obj)); fileIo.close(fileModify); -// 5 读取最新内容 +// 5 Read the latest content let content = fileIo.readTextSync(filePath); console.info(`ModifySanFileContent content is :${content}`); // [End UpdateSandboxJson] \ No newline at end of file -- Gitee