1/* 2 * Copyright (c) 2023-2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import storageStatistics from '@ohos.file.storageStatistics'; 17import fs from '@ohos.file.fs'; 18import { Options } from '@ohos.file.fs'; 19import { BusinessError } from '@ohos.base'; 20import Logger from '../../utils/Logger'; 21import { FileType, SubDirectoryType } from '../../mock/local/FileData'; 22import prompt from '@ohos.promptAction'; 23 24class NewFileSystem { 25 // 创建多级目录 26 createDirectory(filePath: string): void { 27 try { 28 console.log("To create recursive path"); 29 fs.mkdirSync(filePath,true); 30 } catch (err) { 31 Logger.error(`create directory failed, code is ${err.code}, message is ${err.message}`); 32 } 33 } 34 35 // 修改文件时间 36 setFileTime(filePath: string, timeStr: string): void { 37 try { 38 let time = new Date(timeStr).getTime(); 39 if (Number.isNaN(time)) { 40 return prompt.showToast({ message: $r('app.string.tip_invalid_time') }); 41 } 42 fs.utimes(filePath,time); 43 } catch (err) { 44 Logger.error(`setFileTime failed, code is ${err.code}, message is ${err.message}`); 45 throw new Error(`setFileTime failed, code is ${err.code}, message is ${err.message}`); 46 } 47 } 48 49 // 读取文件内容 50 async ReadFile(filePath: string):Promise<string> { 51 try{ 52 let content : string=''; 53 let options: Options = { 54 encoding: 'utf-8' 55 }; 56 await fs.readLines(filePath, options).then((readerIterator: fs.ReaderIterator) => { 57 for (let it = readerIterator.next(); !it.done; it = readerIterator.next()) { 58 content += it.value; 59 } 60 } 61 ).catch((err: BusinessError) => { 62 console.info("readLines failed with error message: " + err.message + ", error code: " + err.code); 63 }); 64 return content; 65 } catch (err) { 66 Logger.error(`OpenFile failed, code is ${err.code}, message is ${err.message}`); 67 throw new Error(`OpenFile failed, code is ${err.code}, message is ${err.message}`); 68 } 69 } 70} 71 72export default new NewFileSystem(); 73