鸿蒙在 ETS (Extendable TypeScript) 中创建多级目录或文件,可以使用鸿蒙的文件系统 API。
import fs from '@ohos.file.fs' ; const TAG= "Index"
@Entry
@Component
struct Index { @State message: string = 'Hello World' ; build ( ) { Row ( ) { Column ( ) { Text ( this. message) . fontSize ( 50 ) . fontWeight ( FontWeight. Bold) Button ( "创建目录" ) . margin ( { top: 10 } ) . onClick ( ( ) = > { this. createDirsByRelativePath ( 'path/to/your/dir' ) ; } ) Button ( "创建文件" ) . margin ( { top: 10 } ) . onClick ( ( ) = > { this. createFile ( 'path/to/your/dir/a.txt' ) ; } ) } . width ( '100%' ) } . height ( '100%' ) } async createFile ( featurePath: string) { let mFeaturePath = "" ; if ( featurePath. indexOf ( "/" ) != - 1 ) { let lastAIndex = featurePath. lastIndexOf ( '/' ) ; let fFileDir: string = featurePath. slice ( 0 , lastAIndex) ; let fileName: string = featurePath. slice ( lastAIndex + 1 ) ; console. info ( TAG, "arrPath:" + fFileDir) ; if ( fFileDir. length != 0 ) { await this. createDirsByRelativePath ( fFileDir) ; mFeaturePath = getContext ( this) . filesDir + fFileDir + "/" + fileName; } else { mFeaturePath = getContext ( this) . filesDir + "/" + fileName; } } else { mFeaturePath = getContext ( this) . filesDir + "/" + featurePath; } console. info ( TAG, "mFeaturePath:" + mFeaturePath) ; } async createMultiDirs ( dirPath: string) : Promise< void > { try { let isExist = await fs. access ( dirPath) ; if ( isExist) { console. info ( `Directory already exists: ${ dirPath} `) ; return ; } } catch ( err) { } const parentDir = dirPath. substring ( 0 , dirPath. lastIndexOf ( '/' ) ) ; if ( parentDir && parentDir != = '/' && parentDir != = '' ) { try { await this. createMultiDirs ( parentDir) ; } catch ( err) { console. error ( `Failed to create parent directory: ${ parentDir} , error: ${ JSON. stringify ( err) } `) ; } } try { await fs. mkdir ( dirPath) ; console. info ( `Directory created: ${ dirPath} `) ; } catch ( err) { if ( err. code != = 13900015 ) { console. error ( `Failed to create directory: ${ dirPath} , error: ${ JSON. stringify ( err) } `) ; } } } async createDirsByRelativePath ( relativePath: string) : Promise< void > { const context = getContext ( ) ; const baseDir = context. filesDir; const targetDir = baseDir + '/' + relativePath; try { await this. createMultiDirs ( targetDir) ; console. info ( 'All directories created successfully. ') ; } catch ( err) { console. error ( `Failed to create directories: ${ JSON. stringify ( err) } `) ; } } async createDirs ( basePath: string, relativePath: string) : Promise< boolean> { const fullPath = basePath + '/' + relativePath; try { await fs. mkdir ( fullPath) ; console. info ( `目录创建成功: ${ fullPath} `) ; return true; } catch ( error) { console. error ( `目录创建失败: ${ JSON. stringify ( error) } `) ; return false; } } async createFileWithPath ( basePath: string, filePath: string, content: string = '' ) : Promise< boolean> { const fullPath = basePath + '/' + filePath; try { const lastSlashIndex = filePath. lastIndexOf ( '/' ) ; const dirPath = lastSlashIndex > 0 ? filePath. substring ( 0 , lastSlashIndex) : '' ; if ( dirPath) { await this. createDirs ( basePath, dirPath) ; } const file = await fs. open ( fullPath, fs. OpenMode. CREATE | fs. OpenMode. READ_WRITE) ; if ( content) { await fs. write ( file. fd, content) ; } await fs. close ( file. fd) ; console. info ( `文件创建成功: ${ fullPath} `) ; return true; } catch ( error) { console. error ( `文件创建失败: ${ JSON. stringify ( error) } `) ; return false; } } async fileExists ( basePath: string, filePath: string) : Promise< boolean> { const fullPath = basePath + '/' + filePath; try { await fs. access ( fullPath) ; return true; } catch { return false; } }
}