// pages/xxx.etsimport{fileIoasfs,ReadOptions}from'@kit.CoreFileKit';import{common}from'@kit.AbilityKit';import{buffer}from'@kit.ArkTS';// Get the application file pathletcontext=getContext(this)ascommon.UIAbilityContext;letfilesDir=context.filesDir;functioncreateFile():void{// Create and open the file if it does not exist; open the file if it existsletfile=fs.openSync(filesDir+'/test.txt',fs.OpenMode.READ_WRITE|fs.OpenMode.CREATE);// Write content to the fileletwriteLen=fs.writeSync(file.fd,"Try to write str.");console.info("The length of str is: "+writeLen);// Create an ArrayBuffer object with a size of 1024 bytes to store data read from the fileletarrayBuffer=newArrayBuffer(1024);// Set the offset and length for readingletreadOptions:ReadOptions={offset:0,length:arrayBuffer.byteLength};// Read the file content into the ArrayBuffer object and return the actual number of bytes readletreadLen=fs.readSync(file.fd,arrayBuffer,readOptions);// Convert the ArrayBuffer object to a Buffer object and output as a stringletbuf=buffer.from(arrayBuffer,0,readLen);console.info("the content of file: "+buf.toString());// Close the filefs.closeSync(file);}
Reading from One File and Writing to Another
// pages/xxx.etsimport{fileIoasfs,ReadOptions,WriteOptions}from'@kit.CoreFileKit';import{common}from'@kit.AbilityKit';// Get the application file pathletcontext=getContext(this)ascommon.UIAbilityContext;letfilesDir=context.filesDir;functionreadWriteFile():void{// Open the source and destination filesletsrcFile=fs.openSync(filesDir+'/test.txt',fs.OpenMode.READ_WRITE|fs.OpenMode.CREATE);letdestFile=fs.openSync(filesDir+'/destFile.txt',fs.OpenMode.READ_WRITE|fs.OpenMode.CREATE);// Read content from the source file and write it to the destination fileletbufSize=4096;letreadSize=0;letbuf=newArrayBuffer(bufSize);letreadOptions:ReadOptions={offset:readSize,length:bufSize};letreadLen=fs.readSync(srcFile.fd,buf,readOptions);while (readLen>0){readSize+=readLen;letwriteOptions:WriteOptions={length:readLen};fs.writeSync(destFile.fd,buf,writeOptions);readOptions.offset=readSize;readLen=fs.readSync(srcFile.fd,buf,readOptions);}// Close the filesfs.closeSync(srcFile);fs.closeSync(destFile);}
Reading/Writing Files Using Streams
// pages/xxx.etsimport{fileIoasfs,ReadOptions}from'@kit.CoreFileKit';import{common}from'@kit.AbilityKit';// Get the application file pathletcontext=getContext(this)ascommon.UIAbilityContext;letfilesDir=context.filesDir;asyncfunctionreadWriteFileWithStream():Promise<void>{// Create and open the input file streamletinputStream=fs.createStreamSync(filesDir+'/test.txt','r+');// Create and open the output file streamletoutputStream=fs.createStreamSync(filesDir+'/destFile.txt',"w+");letbufSize=4096;letreadSize=0;letbuf=newArrayBuffer(bufSize);letreadOptions:ReadOptions={offset:readSize,length:bufSize};// Read content from the source file and write it to the destination file using streamsletreadLen=awaitinputStream.read(buf,readOptions);readSize+=readLen;while (readLen>0){constwriteBuf=readLen<bufSize?buf.slice(0,readLen):buf;awaitoutputStream.write(writeBuf);readOptions.offset=readSize;readLen=awaitinputStream.read(buf,readOptions);readSize+=readLen;}// Close the file streamsinputStream.closeSync();outputStream.closeSync();}
Listing Files
import{fileIoasfs,Filter,ListFileOptions}from'@kit.CoreFileKit';import{common}from'@kit.AbilityKit';// Get the application file pathletcontext=getContext(this)ascommon.UIAbilityContext;letfilesDir=context.filesDir;// List filesfunctiongetListFile():void{letlistFileOption:ListFileOptions={recursion:false,listNum:0,filter:{suffix:[".png",".jpg",".txt"],displayName:["test*"],fileSizeOver:0,lastModifiedAfter:newDate(0).getTime()}};letfiles=fs.listFileSync(filesDir,listFileOption);for (leti=0;i<files.length;i++){console.info(`The name of file: ${files[i]}`);}}
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)