diff --git a/data/EtsFileIo/README_zh.md b/data/EtsFileIo/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..0099ed8851ba213e9895c79e36b5e94253da097f --- /dev/null +++ b/data/EtsFileIo/README_zh.md @@ -0,0 +1,21 @@ +# 文档管理 + +### 简介 + +本示例展示了文档管理的部分功能,例如:创建文件、修改文件名、文件写入、文件读取、删除文件。 + +### 使用说明 + +1.点击"Create fileio"按钮,创建文件,创建成功,Toast显示文件的绝对路径; + +2.点击"Rename fileio"按钮,修改文件名,修改成功,Toast显示修改文件名后的绝对路径; + +3.点击"Write fileio"按钮,向文件中写入内容,写入成功,Toast显示文件写入的内容; + +4.点击"Read fileio"按钮,读取文件中的内容,读取成功,文本框中显示读取到的内容; + +5.点击"Delete fileio"按钮,删除文件,Toast显示文件删除成功。 + +### 约束与限制 + +本示例仅支持在标准系统上运行。 \ No newline at end of file diff --git a/data/EtsFileIo/build.gradle b/data/EtsFileIo/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..c2c8bbaed13747b913f19cef893f515bc32d02a7 --- /dev/null +++ b/data/EtsFileIo/build.gradle @@ -0,0 +1,34 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. +apply plugin: 'com.huawei.ohos.app' + +//For instructions on signature configuration, see https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ide_debug_device-0000001053822404#section1112183053510 +ohos { + compileSdkVersion 7 + supportSystem "standard" +} + +buildscript { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + } + dependencies { + classpath 'com.huawei.ohos:hap:3.0.3.4' + classpath 'com.huawei.ohos:decctest:1.2.6.0' + } +} + +allprojects { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + } +} diff --git a/data/EtsFileIo/entry/build.gradle b/data/EtsFileIo/entry/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..1587dd1948941f3eaaf092ae6cae7969cb6895ff --- /dev/null +++ b/data/EtsFileIo/entry/build.gradle @@ -0,0 +1,21 @@ +apply plugin: 'com.huawei.ohos.hap' +//For instructions on signature configuration, see https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ide_debug_device-0000001053822404#section1112183053510 +ohos { + compileSdkVersion 7 + defaultConfig { + compatibleSdkVersion 7 + } + buildTypes { + release { + proguardOpt { + proguardEnabled false + rulesFiles 'proguard-rules.pro' + } + } + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar', '*.har']) + testImplementation 'junit:junit:4.13.1' +} diff --git a/data/EtsFileIo/entry/src/main/config.json b/data/EtsFileIo/entry/src/main/config.json new file mode 100644 index 0000000000000000000000000000000000000000..8a0252f68d8166eacf4c1da179d4f0a25cb11a5e --- /dev/null +++ b/data/EtsFileIo/entry/src/main/config.json @@ -0,0 +1,66 @@ +{ + "app": { + "bundleName": "ohos.samples.etsfileio", + "vendor": "samples", + "version": { + "code": 1000000, + "name": "1.0.0" + } + }, + "deviceConfig": {}, + "module": { + "package": "ohos.samples.etsfileio", + "name": ".MyApplication", + "mainAbility": ".MainAbility", + "deviceType": [ + "phone" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry", + "installationFree": false + }, + "abilities": [ + { + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + "orientation": "unspecified", + "visible": true, + "srcPath": "MainAbility", + "name": ".MainAbility", + "srcLanguage": "ets", + "icon": "$media:icon", + "description": "$string:description_mainability", + "formsEnabled": false, + "label": "$string:app_name", + "type": "page", + "launchType": "standard" + } + ], + "js": [ + { + "mode": { + "syntax": "ets", + "type": "pageAbility" + }, + "pages": [ + "pages/index" + ], + "name": ".MainAbility", + "window": { + "designWidth": 720, + "autoDesignWidth": false + } + } + ] + } +} \ No newline at end of file diff --git a/data/EtsFileIo/entry/src/main/ets/MainAbility/app.ets b/data/EtsFileIo/entry/src/main/ets/MainAbility/app.ets new file mode 100644 index 0000000000000000000000000000000000000000..bf28e58b36cf11db491d3f00768ef2c9e41d05cf --- /dev/null +++ b/data/EtsFileIo/entry/src/main/ets/MainAbility/app.ets @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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, + * 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. + */ + +export default { + onCreate() { + console.info('Application onCreate') + }, + onDestroy() { + console.info('Application onDestroy') + }, +} \ No newline at end of file diff --git a/data/EtsFileIo/entry/src/main/ets/MainAbility/pages/index.ets b/data/EtsFileIo/entry/src/main/ets/MainAbility/pages/index.ets new file mode 100644 index 0000000000000000000000000000000000000000..c43ef41e68b1df21a49ccefa425e35ac0c6f28c4 --- /dev/null +++ b/data/EtsFileIo/entry/src/main/ets/MainAbility/pages/index.ets @@ -0,0 +1,188 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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, + * 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. + */ + +import fileio from '@ohos.fileio' +import featureAbility from '@ohos.ability.featureAbility' +import prompt from '@system.prompt' +import resourceManager from '@ohos.resourceManager' + +const writeContent = 'hello, fileio' +var context = featureAbility.getContext() +var path = '' +var stream +context.getOrCreateLocalDir().then((e) => { + path = e + '/files' + console.log('EtsFileIo =' + path) +}) + +@Entry +@Component +struct Index { + @State result: string = '' + @State newPath: string = '' + @State readText: string = '' + @State createStr: string = '' + @State renameStr: string = '' + @State writeStr: string = '' + @State readStr: string = '' + @State deleteStr: string = '' + @State fd: number = 0 + private options: Resource [] = [$r('app.string.create_fileio'), $r('app.string.rename_fileio'), + $r('app.string.write_fileio'), $r('app.string.read_fileio'), $r('app.string.delete_fileio')] + + aboutToAppear(){ + resourceManager.getResourceManager().then(mgr =>{ + mgr.getString(0x1000002,(error, value) =>{ + console.log('ResourceManager:getString createStr error = ' + error + ',value = ' + value) + if(error != null){ + return + } + this.createStr = value + }) + mgr.getString(0x1000009,(error, value) =>{ + console.log('ResourceManager:getString renameStr error = ' + error + ',value = ' + value) + if(error != null){ + return + } + this.renameStr = value + }) + mgr.getString(0x100000c,(error, value) =>{ + console.log('ResourceManager:getString writeStr error = ' + error + ',value = ' + value) + if(error != null){ + return + } + this.writeStr = value + }) + mgr.getString(0x1000007,(error, value) =>{ + console.log('ResourceManager:getString readStr error = ' + error + ',value = ' + value) + if(error != null){ + return + } + this.readStr = value + }) + mgr.getString(0x1000004,(error, value) =>{ + console.log('ResourceManager:getString deleteStr error = ' + error + ',value = ' + value) + if(error != null){ + return + } + this.deleteStr = value + }) + }) + } + + createFileIo() { + this.result = path + '/test.txt' + this.fd = fileio.openSync(this.result, 0o2 | 0o100, 0o666) + prompt.showToast({ message: this.createStr + this.result, duration: 2000 }) + console.log('EtsFileIo fd =' + this.fd) + } + + renameFileIo() { + this.newPath = path + '/EtsFileIo.txt' + fileio.renameSync(this.result, this.newPath) + prompt.showToast({ message: this.renameStr + this.newPath, duration: 2000 }) + } + + writeFileIo() { + stream = fileio.createStreamSync(this.newPath, "r+") + let writelength = stream.writeSync(writeContent, { + offset: 0, + length: writeContent.length, + position: 0, + encoding: 'utf-8' + }) + prompt.showToast({ message: this.writeStr + writeContent, duration: 2000 }) + console.log('EtsFileIo WriteLength = ' + writelength) + } + + readFileIo() { + let fdio = fileio.openSync(this.newPath, 0o2) + console.log('EtsFileIo fdio = ' + fdio) + let buf = new ArrayBuffer(4096) + let length = stream.readSync(buf, { offset: 0, length: writeContent.length, position: 0 }) + this.readText = String.fromCharCode.apply(null, new Uint8Array(buf)) + prompt.showToast({ message: this.readStr, duration: 2000 }) + console.log('EtsFileIo buf = ' + this.readText) + console.log('EtsFileIo length = ' + length) + } + + deleteFileIo() { + fileio.unlinkSync(this.newPath) + prompt.showToast({ message: this.deleteStr, duration: 2000 }) + } + + build() { + Column() { + Text($r("app.string.title")) + .width('100%') + .height(50) + .backgroundColor('#0D9FFB') + .fontColor(Color.White) + .fontSize(20) + .padding({ left: 15 }) + Scroll() { + Column() { + ForEach(this.options, item => { + Button() { + Text(item) + .fontSize(19) + .fontWeight(FontWeight.Bold) + .padding({ left: 10, right: 10 }) + .width('100%') + .textAlign(TextAlign.Center) + }.type(ButtonType.Capsule) + .backgroundColor('#0D9FFB') + .margin({ top: 15 }) + .onClick(() => { + var index = this.options.indexOf(item) + switch (index) { + case 0: + this.createFileIo() + break + case 1: + this.renameFileIo() + break + case 2: + this.writeFileIo() + break + case 3: + this.readFileIo() + break + case 4: + this.deleteFileIo() + break + default: + prompt.showToast({ message: 'error', duration: 2000 }) + break + } + }) + }, item => JSON.stringify(item)) + + Text(this.readText) + .width('100%') + .height(200) + .backgroundColor('#DFDFDF') + .fontSize(20) + .margin({ top: 20 }) + .textAlign(TextAlign.Start) + .padding(10) + } + .constraintSize({ minHeight: '100%' }) + } + .width('100%').height('100%') + .padding(15) + } + } +} \ No newline at end of file diff --git a/data/EtsFileIo/entry/src/main/resources/base/element/string.json b/data/EtsFileIo/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..8831d0490dc4349c59aab380a893a9516370ff60 --- /dev/null +++ b/data/EtsFileIo/entry/src/main/resources/base/element/string.json @@ -0,0 +1,56 @@ +{ + "string": [ + { + "name": "app_name", + "value": "eTSFileIO" + }, + { + "name": "description_mainability", + "value": "ETS_Empty Ability" + }, + { + "name": "title", + "value": "eTSFileIO" + }, + { + "name": "create_fileio", + "value": "Create fileio" + }, + { + "name": "rename_fileio", + "value": "Rename fileio" + }, + { + "name": "write_fileio", + "value": "Write fileio" + }, + { + "name": "delete_fileio", + "value": "Delete fileio" + }, + { + "name": "read_fileio", + "value": "Read fileio" + }, + { + "name": "create_string", + "value": "create_file success =>" + }, + { + "name": "rename_string", + "value": "rename_file success =>" + }, + { + "name": "write_string", + "value": "write_file success =>" + }, + { + "name": "read_string", + "value": "read_file success" + }, + { + "name": "delete_string", + "value": "delete_file success" + } + ] +} \ No newline at end of file diff --git a/data/EtsFileIo/entry/src/main/resources/base/media/icon.png b/data/EtsFileIo/entry/src/main/resources/base/media/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..ce307a8827bd75456441ceb57d530e4c8d45d36c Binary files /dev/null and b/data/EtsFileIo/entry/src/main/resources/base/media/icon.png differ diff --git a/data/EtsFileIo/entry/src/main/resources/en/element/string.json b/data/EtsFileIo/entry/src/main/resources/en/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..2d13486730ef0fe3e62494fdfa41487220590e25 --- /dev/null +++ b/data/EtsFileIo/entry/src/main/resources/en/element/string.json @@ -0,0 +1,56 @@ +{ + "string": [ + { + "name": "app_name", + "value": "eTSFileIO" + }, + { + "name": "description_mainability", + "value": "ETS_Empty Ability" + }, + { + "name": "title", + "value": "eTSFileIO" + }, + { + "name": "create_fileio", + "value": "Create file" + }, + { + "name": "rename_fileio", + "value": "Rename file" + }, + { + "name": "write_fileio", + "value": "Write file" + }, + { + "name": "delete_fileio", + "value": "Delete file" + }, + { + "name": "read_fileio", + "value": "Read file" + }, + { + "name": "create_string", + "value": "create_file success =>" + }, + { + "name": "rename_string", + "value": "rename_file success =>" + }, + { + "name": "write_string", + "value": "write_file success =>" + }, + { + "name": "read_string", + "value": "read_file success" + }, + { + "name": "delete_string", + "value": "delete_file success" + } + ] +} \ No newline at end of file diff --git a/data/EtsFileIo/entry/src/main/resources/zh/element/string.json b/data/EtsFileIo/entry/src/main/resources/zh/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..c81069176061c553175561c010112242484874cb --- /dev/null +++ b/data/EtsFileIo/entry/src/main/resources/zh/element/string.json @@ -0,0 +1,56 @@ +{ + "string": [ + { + "name": "app_name", + "value": "文件管理" + }, + { + "name": "description_mainability", + "value": "ETS_Empty Ability" + }, + { + "name": "title", + "value": "文件管理" + }, + { + "name": "create_fileio", + "value": "创建文件" + }, + { + "name": "rename_fileio", + "value": "修改文件名" + }, + { + "name": "write_fileio", + "value": "文件写入" + }, + { + "name": "delete_fileio", + "value": "删除文件" + }, + { + "name": "read_fileio", + "value": "读取文件" + }, + { + "name": "create_string", + "value": "创建文件成功 =>" + }, + { + "name": "rename_string", + "value": "修改文件名成功 =>" + }, + { + "name": "write_string", + "value": "写入文件成功 =>" + }, + { + "name": "read_string", + "value": "读取文件成功" + }, + { + "name": "delete_string", + "value": "删除文件成功" + } + ] +} \ No newline at end of file diff --git a/data/EtsFileIo/screenshots/device/etsFileIo.png b/data/EtsFileIo/screenshots/device/etsFileIo.png new file mode 100644 index 0000000000000000000000000000000000000000..56d8f36fd23c44c9cecae040f05a66cb7b4a67b3 Binary files /dev/null and b/data/EtsFileIo/screenshots/device/etsFileIo.png differ diff --git a/data/EtsFileIo/settings.gradle b/data/EtsFileIo/settings.gradle new file mode 100644 index 0000000000000000000000000000000000000000..4773db73233a570c2d0c01a22e75321acfbf7a07 --- /dev/null +++ b/data/EtsFileIo/settings.gradle @@ -0,0 +1 @@ +include ':entry'