HarmonyOS鸿蒙系统上,file文件常用操作记录
1.创建文件
createFile ( fileName: string, content: string) : string { let context = getContext ( this ) as common. UIAbilityContext; let filesDirPath = context. filesDir + '/' + fileName; let file = fs. openSync ( filesDirPath, fs. OpenMode. READ_WRITE | fs. OpenMode. CREATE ) ; let writeLen = fs. writeSync ( file. fd, content) ; let buf = new ArrayBuffer ( 1024 ) ; let readLen = fs. readSync ( file. fd, buf, { offset: 0 } ) ; fs. closeSync ( file) ; return filesDirPath}
2.将沙箱文件移动到分布式文件夹
async createDistributedFile ( uri: string ) { let context = getContext ( this ) as common. UIAbilityContext; let filesDir = context. distributedFilesDir; let fileName = this . getFileName ( uri) try { let destUriPath = filesDir + '/' + fileNamethis . tempFilePath = fileNamelet writeFile = fs. openSync ( destUriPath, fs. OpenMode. READ_WRITE | fs. OpenMode. CREATE ) ; securityLabel. setSecurityLabel ( destUriPath, 's1' ) . then ( ( ) => { } ) const file = fs. openSync ( uri, fs. OpenMode. READ_ONLY ) ; let photoSize = fs. statSync ( file. fd) . size; let buffer = new ArrayBuffer ( photoSize) ; fs. readSync ( file. fd, buffer) ; fs. closeSync ( file) ; let writeLen = fs. write ( writeFile. fd, buffer) } catch ( error) { } }
3.读取分布式文件
readDistributedFile ( filePath: string ) { let context = getContext ( this ) as common. UIAbilityContext; let distributedFilesDir = context. distributedFilesDir; let distributedFilesDirs = fs. listFileSync ( distributedFilesDir) ; for ( let i = 0 ; i < distributedFilesDirs. length; i++ ) { let fileName = distributedFilesDir + '/' + distributedFilesDirs[ i] this . readFile ( fileName) } }
4.读取文件内容
readFile ( filePath: string ) { try { let file = fs. openSync ( filePath, fs. OpenMode. READ_WRITE ) ; let arrayBuffer = new ArrayBuffer ( 4096 ) ; class Option { public offset: number = 0 ; public length: number = 0 ; } let option = new Option ( ) ; option. length = arrayBuffer. byteLength; let num = fs. readSync ( file. fd, arrayBuffer, option) ; let buf = buffer. from ( arrayBuffer, 0 , num) ; Log. info ( '读取的文件内容: ' + buf. toString ( ) ) ; } catch ( error) { let err: BusinessError = error as BusinessError; Log. error ( ` Failed to openSync / readSync. Code: ${ err. code} , message: ${ err. message} ` ) ; } }
5.产看文件列表(沙箱和分布式文件夹)
lookFilesList ( ) : string { let allFiles = '' let context = getContext ( this ) as common. UIAbilityContext; let filesDir = context. filesDir; let files = fs. listFileSync ( filesDir) ; for ( let i = 0 ; i < files. length; i++ ) { Log. info ( ` 当前设备文件 name: ${ files[ i] } ` ) ; } let distributedFilesDir = context. distributedFilesDir; Log. info ( 'context.distributedFilesDir: ' + distributedFilesDir) let distributedFilesDirs = fs. listFileSync ( distributedFilesDir) ; if ( distributedFilesDirs. length > 0 ) { for ( let i = 0 ; i < distributedFilesDirs. length; i++ ) { Log. info ( ` 分布式文件 name: ${ distributedFilesDirs[ i] } ` ) ; } } return allFiles; }
6.删除分布式下指定文件
deleteDistributedFile ( fileName: string ) { let context = getContext ( this ) as common. UIAbilityContext; let filePath = context. distributedFilesDir + '/' + fileNamesecurityLabel. setSecurityLabel ( filePath, 's1' ) . then ( ( ) => { Log. info ( 'Succeeded in setSecurityLabeling.' ) ; } ) try { fs. rmdir ( filePath) Log. info ( '刪除文件成功' ) } catch ( error) { let err: BusinessError = error as BusinessError; Log. error ( ` Failed to openSync / readSync. Code: ${ err. code} , message: ${ err. message} ` ) ; } }
7. 删除本地文件
deleteCurrentDeviceFile ( ) { let context = getContext ( this ) as common. UIAbilityContext; let filesDir = context. filesDir; let filesDirs = fs. listFileSync ( filesDir) ; if ( filesDirs. length <= 0 ) { return } let fileName = filesDirs + '/' + filesDirs[ 0 ] securityLabel. setSecurityLabel ( fileName, 's1' ) . then ( ( ) => { Log. info ( 'Succeeded in setSecurityLabeling.' ) ; } ) try { fs. rmdir ( fileName) Log. info ( '刪除文件成功' ) } catch ( error) { let err: BusinessError = error as BusinessError; Log. error ( ` Failed to openSync / readSync. Code: ${ err. code} , message: ${ err. message} ` ) ; } }
8.获取文件名
getFileName ( filePath: string) : string { const parts = filePath. split ( '/' ) ; if ( parts. length === 0 ) { return '' ; } const fileNameWithExtension = parts[ parts. length - 1 ] ; const fileNameParts = fileNameWithExtension. split ( '.' ) ; if ( fileNameParts. length < 2 ) { return fileNameWithExtension; } return fileNameWithExtension; }
9.保存文件到本地文件夹
async saveFile ( fileName: string) : Promise< string> { let saveFileUri = '' try { let DocumentSaveOptions = new picker. DocumentSaveOptions ( ) ; DocumentSaveOptions. newFileNames = [ fileName] ; let documentPicker = new picker. DocumentViewPicker ( ) ; await documentPicker. save ( DocumentSaveOptions) . then ( ( DocumentSaveResult ) => { if ( DocumentSaveResult !== null && DocumentSaveResult !== undefined ) { let uri = DocumentSaveResult[ 0 ] as string; this . realFileUri = urithis . realSaveFile ( fileName) saveFileUri = uri} } ) . catch ( ( err: BusinessError ) => { Log. error ( 'saveFile-DocumentViewPicker.save failed with err: ' + JSON . stringify ( err) ) ; } ) ; } catch ( err) { Log. error ( 'saveFile-DocumentViewPicker failed with err: ' + err) ; } return saveFileUri} realFileUri: string = '' async realSaveFile ( fileName: string ) { let context = getContext ( this ) as common. UIAbilityContext; let pathDir = context. distributedFilesDir; let filePath = pathDir + '/' + fileName; const file = fs. openSync ( filePath, fs. OpenMode. READ_ONLY ) ; let photoSize = fs. statSync ( file. fd) . size; let buffer = new ArrayBuffer ( photoSize) ; fs. readSync ( file. fd, buffer) ; fs. closeSync ( file) ; let saveFile = fs. openSync ( this . realFileUri, fs. OpenMode. WRITE_ONLY ) ; let writeLen = fs. write ( saveFile. fd, buffer) Log. info ( ` save file uri success ~~~~~ ` + writeLen) ; this . deleteDistributedFile ( filePath) }