From 49e9490e338e2a13dc702afa534951371a8c53f1 Mon Sep 17 00:00:00 2001 From: "1362134550@qq.com" <1362134550@qq.com> Date: Tue, 16 Sep 2025 16:31:42 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=BC=82=E5=B8=B8=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/pages/Index.ets | 2 +- entry/src/main/ets/view/File.ets | 42 +++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 69a62dc..468c434 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -146,7 +146,7 @@ struct Index { try { this.getUIContext().getPromptAction().showToast({ - message: this.getUIContext().getHostContext()!.resourceManager.getStringSync($r('app.string.message')), + message: $r('app.string.message'), duration: 2000 }); } catch (error) { diff --git a/entry/src/main/ets/view/File.ets b/entry/src/main/ets/view/File.ets index 10455e6..b47d2fd 100644 --- a/entry/src/main/ets/view/File.ets +++ b/entry/src/main/ets/view/File.ets @@ -37,9 +37,13 @@ export struct File { lastModifiedAfter: new Date(0).getTime() } }; - let files = fs.listFileSync(filesDir, listFileOption); - for (let i = 0; i < files.length; i++) { - this.fileList = JSON.stringify(files[i]) + try { + let files = fs.listFileSync(filesDir, listFileOption); + for (let i = 0; i < files.length; i++) { + this.fileList = JSON.stringify(files[i]) + } + } catch (e) { + hilog.info(0x0000, 'GenerateSandboxFile', `listFileSync error ${JSON.stringify(e)}`); } let preferencesDir = context.preferencesDir; @@ -53,9 +57,13 @@ export struct File { lastModifiedAfter: new Date(0).getTime() } }; - let preferences = fs.listFileSync(preferencesDir, listPreferencesOption); - for (let i = 0; i < preferences.length; i++) { - this.preferenceList = JSON.stringify(preferences[i]) + try { + let preferences = fs.listFileSync(preferencesDir, listPreferencesOption); + for (let i = 0; i < preferences.length; i++) { + this.preferenceList = JSON.stringify(preferences[i]) + } + } catch (e) { + hilog.info(0x0000, 'GenerateSandboxFile', `listFileSync error ${JSON.stringify(e)}`); } let rdbDir = context.databaseDir; @@ -69,9 +77,13 @@ export struct File { lastModifiedAfter: new Date(0).getTime() } }; - let rdb = fs.listFileSync(rdbDir, listRdbOption); - for (let i = 0; i < rdb.length; i++) { - this.rdbList = JSON.stringify(rdb[i]) + try { + let rdb = fs.listFileSync(rdbDir, listRdbOption); + for (let i = 0; i < rdb.length; i++) { + this.rdbList = JSON.stringify(rdb[i]) + } + } catch (e) { + hilog.info(0x0000, 'GenerateSandboxFile', `listFileSync error ${JSON.stringify(e)}`); } let kvStoreDir = context.databaseDir; @@ -85,9 +97,13 @@ export struct File { lastModifiedAfter: new Date(0).getTime() } }; - let kvStores = fs.listFileSync(kvStoreDir, listKvStoreOption); - for (let i = 0; i < kvStores.length; i++) { - this.kvStoreList = JSON.stringify(kvStores[i]) + try { + let kvStores = fs.listFileSync(kvStoreDir, listKvStoreOption); + for (let i = 0; i < kvStores.length; i++) { + this.kvStoreList = JSON.stringify(kvStores[i]) + } + } catch (e) { + hilog.info(0x0000, 'GenerateSandboxFile', `listFileSync error ${JSON.stringify(e)}`); } } @@ -141,6 +157,6 @@ export struct File { } } } - .title(this.getUIContext().getHostContext()!.resourceManager.getStringSync($r('app.string.tab3'))) + .title($r('app.string.tab3')) } } \ No newline at end of file -- Gitee From 23f80a54e1101d5d76f05329e86dfc80518f8404 Mon Sep 17 00:00:00 2001 From: "1362134550@qq.com" <1362134550@qq.com> Date: Tue, 16 Sep 2025 16:33:18 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E5=BC=82=E5=B8=B8=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/view/File.ets | 1 + 1 file changed, 1 insertion(+) diff --git a/entry/src/main/ets/view/File.ets b/entry/src/main/ets/view/File.ets index b47d2fd..42cd0b1 100644 --- a/entry/src/main/ets/view/File.ets +++ b/entry/src/main/ets/view/File.ets @@ -15,6 +15,7 @@ import { common } from '@kit.AbilityKit'; import { fileIo as fs, ListFileOptions } from '@kit.CoreFileKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; @Entry @Component -- Gitee From 79cc033bb5ef5e2604e5e062fccaec281fd86099 Mon Sep 17 00:00:00 2001 From: "1362134550@qq.com" <1362134550@qq.com> Date: Tue, 16 Sep 2025 16:44:05 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E5=BC=82=E5=B8=B8=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/pages/Index.ets | 85 +++++++++++-------- entry/src/main/ets/view/File.ets | 1 - .../resources/base/profile/main_pages.json | 3 +- 3 files changed, 52 insertions(+), 37 deletions(-) diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 468c434..d8a951e 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -1,13 +1,13 @@ /* * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); + * Licensed under the Apache License, Version 2.0 (the 'License'); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, + * distributed under the License is distributed on an 'AS IS' BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. @@ -19,7 +19,6 @@ import { common } from '@kit.AbilityKit'; import { buffer } from '@kit.ArkTS'; import { BusinessError } from '@kit.BasicServicesKit'; import { File } from '../view/File'; -import { promptAction } from '@kit.ArkUI'; import Logger from '../common/Logger'; @Entry @@ -57,34 +56,42 @@ struct Index { .height(40) .onClick(() => { let context = this.getUIContext().getHostContext() as common.UIAbilityContext; - let filesDir = context.filesDir; - let file = fs.openSync(filesDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); - let writeLen = fs.writeSync(file.fd, "Try to write str."); - Logger.info("The Length of str is: " + writeLen); - let arrayBuffer = new ArrayBuffer(1024); - let readOptions: ReadOptions = { - offset: 0, - length: arrayBuffer.byteLength - }; - let readLen = fs.readSync(file.fd, arrayBuffer, readOptions); - let buf = buffer.from(arrayBuffer, 0, readLen); - Logger.info("the content of file: " + buf.toString()); - fs.closeSync(file); + try { + let filesDir = context.filesDir; + let file = fs.openSync(filesDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); + let writeLen = fs.writeSync(file.fd, 'Try to write str.'); + Logger.info('The Length of str is: ' + writeLen); + let arrayBuffer = new ArrayBuffer(1024); + let readOptions: ReadOptions = { + offset: 0, + length: arrayBuffer.byteLength + }; + let readLen = fs.readSync(file.fd, arrayBuffer, readOptions); + let buf = buffer.from(arrayBuffer, 0, readLen); + Logger.info('the content of file: ' + buf.toString()); + fs.closeSync(file); + } catch (e) { + Logger.error(`file error ${JSON.stringify(e)}`); + } - let options2: preferences.Options = { name: 'myStore' }; - this.dataPreferences = preferences.getPreferencesSync(this.context, options2); - if (this.dataPreferences.hasSync('startup')) { - Logger.info("The key 'startup' is contained."); - } else { - Logger.info("The key 'startup' does not contain."); - this.dataPreferences.putSync('startup', 'auto'); - this.dataPreferences.flush((err: BusinessError) => { - if (err) { - Logger.error(`Failed to flush. Code:${err.code}, message:${err.message}`); - return; - } - Logger.info('Succeeded in flushing.'); - }) + try { + let options2: preferences.Options = { name: 'myStore' }; + this.dataPreferences = preferences.getPreferencesSync(this.context, options2); + if (this.dataPreferences.hasSync('startup')) { + Logger.info('The key "startup" is contained.'); + } else { + Logger.info('The key "startup" does not contain.'); + this.dataPreferences.putSync('startup', 'auto'); + this.dataPreferences.flush((err: BusinessError) => { + if (err) { + Logger.error(`Failed to flush. Code:${err.code}, message:${err.message}`); + return; + } + Logger.info('Succeeded in flushing.'); + }) + } + } catch (e) { + Logger.error(`preferences error ${JSON.stringify(e)}`); } const kvManagerConfig: distributedKVStore.KVManagerConfig = { @@ -109,7 +116,9 @@ struct Index { } Logger.info('Succeeded in getting KVStore.'); kvStore = store; - kvStore.put('test', 'test1') + kvStore.put('test', 'test1').catch((error: BusinessError) => { + Logger.error(`put error ${error.code}`); + }); }); const STORE_CONFIG: relationalStore.StoreConfig = { @@ -129,17 +138,25 @@ struct Index { Logger.info('Succeeded in getting RdbStore.'); if (store.version === 0) { - store.executeSql(SQL_CREATE_TABLE); + store.executeSql(SQL_CREATE_TABLE).catch((error: BusinessError) => { + Logger.error(`executeSql error ${error.code}`); + }); store.version = 3; } if (store.version === 1) { - (store as relationalStore.RdbStore).executeSql('ALTER TABLE EMPLOYEE ADD COLUMN AGE INTEGER'); + (store as relationalStore.RdbStore).executeSql('ALTER TABLE EMPLOYEE ADD COLUMN AGE INTEGER') + .catch((error: BusinessError) => { + Logger.error(`executeSql error ${error.code}`); + }); store.version = 2; } if (store.version === 2) { - (store as relationalStore.RdbStore).executeSql('ALTER TABLE EMPLOYEE DROP COLUMN ADDRESS TEXT'); + (store as relationalStore.RdbStore).executeSql('ALTER TABLE EMPLOYEE DROP COLUMN ADDRESS TEXT') + .catch((error: BusinessError) => { + Logger.error(`executeSql error ${error.code}`); + }); store.version = 3; } }) diff --git a/entry/src/main/ets/view/File.ets b/entry/src/main/ets/view/File.ets index 42cd0b1..2a58e1c 100644 --- a/entry/src/main/ets/view/File.ets +++ b/entry/src/main/ets/view/File.ets @@ -17,7 +17,6 @@ import { common } from '@kit.AbilityKit'; import { fileIo as fs, ListFileOptions } from '@kit.CoreFileKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; -@Entry @Component export struct File { @State fileList: string = ''; diff --git a/entry/src/main/resources/base/profile/main_pages.json b/entry/src/main/resources/base/profile/main_pages.json index 2ed174f..1898d94 100644 --- a/entry/src/main/resources/base/profile/main_pages.json +++ b/entry/src/main/resources/base/profile/main_pages.json @@ -1,6 +1,5 @@ { "src": [ - "pages/Index", - "view/File" + "pages/Index" ] } -- Gitee From 661e031f9d1b5ada44bcb50882113af07ad3ba1c Mon Sep 17 00:00:00 2001 From: "1362134550@qq.com" <1362134550@qq.com> Date: Tue, 16 Sep 2025 16:45:56 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=BC=82=E5=B8=B8=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/pages/Index.ets | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index d8a951e..2411f56 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -1,13 +1,13 @@ /* * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the 'License'); + * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an 'AS IS' BASIS, + * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. -- Gitee