跳到主要内容

bn.getFileSystemManager

getFileSystemManager(): FileSystemManager

description Get the file system manager instance which is a singleton. On Android devices, file system api will save file on external storage by default.- About filePath: filePath should starts from root directory of source code bundle.Only supports absolute path like /a/b or a/b.Files with extensions of js,json,bxss,bxm will be transformed into other files after compilation, so they are not expected to be accessible.

  • Valid value of encoding: utf-8, utf8

example

const manager = bn.getFileSystemManager()
manager.readFile({filePath: 'xxx'})

Returns

FileSystemManager

Class: FileSystemManager

NameTypeDescription
rpcRPC
readFileNativeundefined | (options: ReadFileOption) => Promise<FSReadFileResult>
writeFileNativeundefined | (options: WriteFileOption) => Promise<GeneralCallbackResult>
readFileNativeSyncundefined | (options: any) => any
writeFileNativeSyncundefined | (options: any) => any
readFile(options): Promise<FSReadFileResult>description Read files in local directory.
writeFile(options): Promise<GeneralCallbackResult>description Write data into file in local directory.
mkdir(options): Promise<{}>description Create directory.
access(options): Promise<{}>description Try to access file/directory.
unzip(options): Promise<{}>description Unzip file.
copyFile(options): Promise<{}>description Copy file to another path.
readFileSync(filePath, encoding?, position?, length?): FileDatadescription Sync version of readFile.
writeFileSync(filePath, data, encoding?): anydescription Sync version of writeFile.
mkdirSync(dirPath, recursive?): Objectdescription Sync version of mkdir.
accessSync(path): Objectdescription Sync version of access.
copyFileSync(srcPath, destPath): Objectdescription Sync version of copyFile.

readFile

readFile(options): Promise<FSReadFileResult>

description Read files in local directory.

example

bn.getFileSystemManager().readFile({
filePath: `${bn.env.USER_DATA_PATH}/helloWorld.txt`,
encoding: 'utf-8',
success(res) {
console.log(res)
},
})

Parameters

NameType
optionsReadFileOption

Returns

Promise<FSReadFileResult>


writeFile

writeFile(options): Promise<GeneralCallbackResult>

description Write data into file in local directory.

example

bn.getFileSystemManager().writeFile({
filePath: `${bn.env.USER_DATA_PATH}/helloWorld.txt`,
data: 'hello world'
encoding: 'utf-8',
success(res) {
console.log(res)
},
})

Parameters

NameType
optionsWriteFileOption

Returns

Promise<GeneralCallbackResult>


mkdir

mkdir(options): Promise<{}>

description Create directory.

example

bn.getFileSystemManager().mkdir({
dirPath: 'a/b/c/d',
recursive: true,
success(res) {
console.log(res)
},
})

Parameters

NameType
optionsFsMkdirOptions

Returns

Promise<{}>


access

access(options): Promise<{}>

description Try to access file/directory.

example

bn.getFileSystemManager().access({
path: 'a/b/c/d',
success(res) {
console.log(res)
},
fail(err) {
}
})

Parameters

NameType
optionsFsAccessOptions

Returns

Promise<{}>


unzip

unzip(options): Promise<{}>

description Unzip file.

example

bn.getFileSystemManager().unzip({
zipFilePath: 'foo.zip',
targetPath: 'foo'
success(res) {
console.log(res)
},
})

Parameters

NameType
optionsFsUnzipOptions

Returns

Promise<{}>


copyFile

copyFile(options): Promise<{}>

description Copy file to another path.

example

bn.getFileSystemManager().copyFile({
destPath: 'a/foo',
srcPath: 'a/bar'
success(res) {
console.log(res)
},
})

Parameters

NameType
optionsFsCopyOptions

Returns

Promise<{}>


readFileSync

readFileSync(filePath, encoding?, position?, length?): FileData

description Sync version of readFile.

example

bn.getFileSystemManager().readFileSync(`${bn.env.USER_DATA_PATH}/helloWorld.txt`,'utf-8')
})

Parameters

NameType
filePathstring
encoding?"ascii" | "base64" | "binary" | "hex" | "ucs2" | "ucs-2" | "utf16le" | "utf-16le" | "utf-8" | "utf8" | "latin1"
position?number
length?number

Returns

FileData


writeFileSync

writeFileSync(filePath, data, encoding?): any

description Sync version of writeFile.

example

bn.getFileSystemManager()
.writeFileSync(
`${bn.env.USER_DATA_PATH}/helloWorld.txt`,
'hello world',
'utf-8'
)
})

Parameters

NameType
filePathstring
dataFileData
encoding?"ascii" | "base64" | "binary" | "hex" | "ucs2" | "ucs-2" | "utf16le" | "utf-16le" | "utf-8" | "utf8" | "latin1"

Returns

any


mkdirSync

mkdirSync(dirPath, recursive?): Object

description Sync version of mkdir.

example

bn.getFileSystemManager()
.writeFileSync(
`${bn.env.USER_DATA_PATH}/helloWorld.txt`,
'hello world',
'utf-8'
)
})

Parameters

NameType
dirPathstring
recursive?boolean

Returns

Object


accessSync

accessSync(path): Object

description Sync version of access.

example

bn.getFileSystemManager()
.accessSync(
`${bn.env.USER_DATA_PATH}/helloWorld.txt`
)
})

Parameters

NameType
pathstring

Returns

Object


copyFileSync

copyFileSync(srcPath, destPath): Object

description Sync version of copyFile.

example

bn.getFileSystemManager()
.copyFileSync(
`${bn.env.USER_DATA_PATH}/helloWorld.txt`,
`${bn.env.USER_DATA_PATH}/foo.txt`
)
})

Parameters

NameType
srcPathstring
destPathstring

Returns

Object

Interface: FSReadFileResult

NameTypeDescription
errMsgstringAPI call result
data?FileDataFile content

Interface: ReadFileOption

NameTypeDescription
filePathstringThe file path to read.
encoding?"ascii" | "base64" | "binary" | "hex" | "ucs2" | "ucs-2" | "utf16le" | "utf-16le" | "utf-8" | "utf8" | "latin1"File's character encoding. if not provided, read it in format of ArrayBuffer.; ;
length?numberSpecify the length of file. If not provided, read until the end of file.Valid range: [1, fileLength].Unit: byte.
position?numberRead file starting from specific position.If not provided, start from file head.The reading range should be left-closed and right-open, [position, position+length). Valid range: [0, fileLength - 1].Unit: byte.
complete?(res): voidComplete callback
fail?(err): voidFail callback
success?(res): voidSuccess callback

complete

Optional complete(res): void

Complete callback

Parameters

NameType
resGeneralCallbackResult

Returns

void


fail

Optional fail(err): void

Fail callback

Parameters

NameType
errGeneralCallbackResult

Returns

void


success

Optional success(res): void

Success callback

Parameters

NameType
resReadFileSuccessCallbackResult

Returns

void

Interface: GeneralCallbackResult

NameTypeDescription
errMsg?stringAPI call result

Interface: ReadFileSuccessCallbackResult

NameTypeDescription
dataFileDataFile content
errMsg?stringAPI call result

Interface: GeneralCallbackResult

NameTypeDescription
errMsg?stringAPI call result

WriteFileOption

Ƭ WriteFileOption: Object

Type declaration

NameTypeDescription
completeundefined | (res: GeneralCallbackResult) => void-
failundefined | (err: GeneralCallbackResult) => void-
successundefined | (res: GeneralCallbackResult) => void-
datastring | ArrayBufferThe data to write.
filePathstringThe file path to write data in.
encoding?undefined | "ascii" | "base64" | "binary" | "hex" | "ucs2" | "ucs-2" | "utf16le" | "utf-16le" | "utf-8" | "utf8" | "latin1"Character encoding of the file to be written.

FsMkdirOptions

Ƭ FsMkdirOptions: Object

Type declaration

NameType
dirPathstring
recursiveundefined | boolean
completeundefined | (res: GeneralCallbackResult) => void
failundefined | (err: GeneralCallbackResult) => void
successundefined | (res: GeneralCallbackResult) => void

FsAccessOptions

Ƭ FsAccessOptions: Object

Type declaration

NameType
pathstring
completeundefined | (res: GeneralCallbackResult) => void
failundefined | (err: GeneralCallbackResult) => void
successundefined | (res: GeneralCallbackResult) => void

FsUnzipOptions

Ƭ FsUnzipOptions: Object

Type declaration

NameType
zipFilePathstring
targetPathstring
completeundefined | (res: GeneralCallbackResult) => void
failundefined | (err: GeneralCallbackResult) => void
successundefined | (res: GeneralCallbackResult) => void

FsCopyOptions

Ƭ FsCopyOptions: Object

Type declaration

NameTypeDescription
destPathstring-
srcPathstring-
errMsg?undefined | stringAPI call result

FileData

Ƭ FileData: string | ArrayBuffer