百度智能小程序 FileSystemManager

2020-09-05 14:21 更新

FileSystemManager

解釋:文件管理器。

Web 態(tài)說明:受瀏覽器限制,Web 態(tài)不支持文件系統(tǒng)相關(guān)功能,調(diào)用該對象的方法會執(zhí)行失敗回調(diào)函數(shù)或拋出一個(gè)標(biāo)準(zhǔn)的Error對象。

方法列表

方法 說明
FileSystemManager.access 判斷文件/目錄是否存在
FileSystemManager.accessSync 開判斷文件/目錄是否存在的同步接口
FileSystemManager.appendFile 在文件結(jié)尾追加內(nèi)容
FileSystemManager.appendFileSync 在文件結(jié)尾追加內(nèi)容的同步接口
FileSystemManager.copyFile 用于實(shí)現(xiàn)文件拷貝
FileSystemManager.copyFileSync 用于實(shí)現(xiàn)文件拷貝的同步接口
FileSystemManager.getFileInfo 獲取本地臨時(shí)文件的相關(guān)信息
FileSystemManager.getSavedFileList 獲取該小程序下已保存的本地用戶文件列表
FileSystemManager.mkdir 創(chuàng)建目錄
FileSystemManager.mkdirSync 創(chuàng)建目錄的同步接口
FileSystemManager.readdir 讀取本地指定路徑下的目錄內(nèi)文件列表
FileSystemManager.readdirSync 讀取本地指定路徑下的目錄內(nèi)文件列表的同步接口
FileSystemManager.readFile 讀取本地文件內(nèi)容
FileSystemManager.readFileSync 讀取本地文件內(nèi)容的同步接口
FileSystemManager.removeSavedFile 刪除該小程序下已保存的本地用戶文件
FileSystemManager.rename 重命名文件
FileSystemManager.renameSync 重命名文件的同步接口
FileSystemManager.rmdir 刪除目錄
FileSystemManager.rmdirSync 刪除目錄的同步接口
FileSystemManager.saveFile 保存臨時(shí)文件到本地
FileSystemManager.saveFileSync 保存臨時(shí)文件到本地的同步接口
FileSystemManager.stat 獲取文件 Stats 對象
FileSystemManager.statSync 獲取文件 Stats 對象的同步接口
FileSystemManager.unlink 刪除文件
FileSystemManager.unlinkSync 刪除文件的同步接口
FileSystemManager.unzip 解壓文件
FileSystemManager.writeFile 寫文件
FileSystemManager.writeFileSync 寫文件的同步接口

示例 

在開發(fā)者工具中打開


代碼示例

<view class="wrap">
    <button type="primary" bindtap="access">access</button>
    <button type="primary" bindtap="accessSync">accessSync</button>
    <button type="primary" bindtap="appendFile">appendFile</button>
    <button type="primary" bindtap="appendFileSync">appendFileSync</button>
    <button type="primary" bindtap="copyFile">copyFile</button>
    <button type="primary" bindtap="copyFileSync">copyFileSync</button>
    <button type="primary" bindtap="getFileInfo">getFileInfo</button>
    <button type="primary" bindtap="getSavedFileList">getSavedFileList</button>
    <button type="primary" bindtap="mkdir">mkdir</button>
    <button type="primary" bindtap="mkdirSync">mkdirSync</button>
    <button type="primary" bindtap="readdir">readdir</button>
    <button type="primary" bindtap="readdirSync">readdirSync</button>
    <button type="primary" bindtap="readFile">readFile</button>
    <button type="primary" bindtap="readFileSync">readFileSync</button>
    <button type="primary" bindtap="removeSavedFile">removeSavedFile</button>
    <button type="primary" bindtap="rename">rename</button>
    <button type="primary" bindtap="renameSync">renameSync</button>
    <button type="primary" bindtap="rmdir">rmdir</button>
    <button type="primary" bindtap="rmdirSync">rmdirSync</button>
    <button type="primary" bindtap="saveFile">saveFile</button>
    <button type="primary" bindtap="saveFileSync">saveFileSync</button>
    <button type="primary" bindtap="stat">stat</button>
    <button type="primary" bindtap="statSync">statSync</button>
    <button type="primary" bindtap="unlink">unlink</button>
    <button type="primary" bindtap="unlinkSync">unlinkSync</button>
    <button type="primary" bindtap="unzip">unzip</button>
    <button type="primary" bindtap="writeFile">writeFile</button>
    <button type="primary" bindtap="writeFileSync">writeFileSync</button>
</view>

Page({
    onLoad() {
        const fs = swan.getFileSystemManager();
        this.fs = fs;
    },
    access() {
        this.fs.access({
            path: `${swan.env.USER_DATA_PATH}/demo.txt`,
            success: res => {
                console.log('access success', res);
            },
            fail: err => {
                console.log('access fail', err);
            }
        });
    },
    accessSync() {
        try {
            let result = this.fs.accessSync(
                `${swan.env.USER_DATA_PATH}/demo.txt`
            );
            console.log('accessSync success', result);
        }
        catch (err) {
            console.log('accessSync fail', err);
        }
    },
    appendFile() {
        this.fs.appendFile({
            filePath: `${swan.env.USER_DATA_PATH}/demo.txt`,
            data: 'appendFile',
            success: res => {
                console.log('appendFile success', res);
            },
            fail: err => {
                console.log('appendFile fail', err)
            }
        });
    },
    appendFileSync() {
        try {
            let result = this.fs.appendFileSync(
                `${swan.env.USER_DATA_PATH}/demo.txt`,
                'appendFileSync'
            );
            console.log('appendFileSync success', result);
        }
        catch (err) {
            console.log('appendFileSync fail', err);
        }
    },
    copyFile() {
        this.fs.copyFile({
            srcPath: `${swan.env.USER_DATA_PATH}/demo.txt`,
            destPath: `${swan.env.USER_DATA_PATH}/destPath.txt`,
            success: res => {
                console.log('copyFile success', res);
            },
            fail: err => {
                console.log('copyFile fail', err);
            }
        });
    },
    copyFileSync() {
        try {
            let result = this.fs.copyFileSync(
                `${swan.env.USER_DATA_PATH}/demo.txt`,
                `${swan.env.USER_DATA_PATH}/copyFileSync.txt`
            );
            console.log('copyFileSync success', result);
        }
        catch (err) {
            console.log('copyFileSync fail', err);
        }
    },
    getFileInfo() {
        this.fs.getFileInfo({
            filePath: `${swan.env.USER_DATA_PATH}/demo.txt`,
            success: res => {
                console.log('getFileInfo success', res);
            },
            fail: err => {
                console.log('getFileInfo fail', err);
            }
        });
    },
    getSavedFileList() {
        this.fs.getSavedFileList({
            success: res => {
                console.log('getSavedFileList success', res);
            },
            fail: err => {
                console.log('getSavedFileList fail', err)
            }
        });
    },
    mkdir() {
        this.fs.mkdir({
            dirPath: `${swan.env.USER_DATA_PATH}/mkdir`,
            success: res => {
                console.log('mkdir success', res);
            },
            fail: err => {
                console.log('mkdir fail', err);
            }
        });
    },
    mkdirSync() {
        try {
            let result = this.fs.mkdirSync(
                `${swan.env.USER_DATA_PATH}/mkdirSyc`
            );
            console.log('mkdirSync success', result);
        }
        catch (err) {
            console.log('mkdirSync fail', err);
        }
    },
    readdir() {
        this.fs.readdir({
            dirPath: `${swan.env.USER_DATA_PATH}/readdir`,
            success: res => {
                console.log('readdir success', res);
            },
            fail: err => {
                console.log('readdir fail', err);
            }
        });
    },
    readdirSync() {
        try {
            let result = this.fs.readdirSync(
                `${swan.env.USER_DATA_PATH}/readdirSyc'`
            );
            console.log('readdirSync success', result);
        }
        catch (err) {
            console.log('readdirSync fail', err);
        }
    },
    readFile() {
        this.fs.readFile({
            filePath: `${swan.env.USER_DATA_PATH}/demo.txt`,
            encoding: 'utf8',
            success: res => {
                console.log('readFile success', res);
            },
            fail: err => {
                console.log('readFile fail', err);
            }
        });
    },
    readFileSync() {
        try {
            let result = this.fs.readFileSync(
                `${swan.env.USER_DATA_PATH}/demo.txt`
            );
            console.log('readFileSync success', result);
        }
        catch (err) {
            console.log('readFileSync fail', err);
        }
    },
    removeSavedFile() {
        this.fs.removeSavedFile({
            filePath: `${swan.env.USER_DATA_PATH}/demo.txt`,
            encoding: 'utf8',
            success: res => {
                console.log('removeSavedFile success', res);
            },
            fail: err => {
                console.log('removeSavedFile fail', err)
            }
        });
    },
    rename() {
        this.fs.rename({
            oldPath: `${swan.env.USER_DATA_PATH}/demo.txt`,
            newPath: `${swan.env.USER_DATA_PATH}/rename/demo.txt`,
            encoding: 'utf8',
            success: res => {
                console.log('rename success', res);
            },
            fail: err => {
                console.log('readdir fail', err);
            }
        });
    },
    renameSync() {
        try {
            let result = this.fs.renameSync(
                `${swan.env.USER_DATA_PATH}/demo.txt`,
                `${swan.env.USER_DATA_PATH}/renameSync/demo.txt`
            );
            console.log('renameSync success', result);
        }
        catch (err) {
            console.log('renameSync fail', err);
        }
    },
    rmdir() {
        this.fs.rmdir({
            dirPath: `${swan.env.USER_DATA_PATH}/rmdir`,
            success: res => {
                console.log('rmdir success', res);
            },
            fail: err => {
                console.log('rmdir fail', err);
            }
        });
    },
    rmdirSync() {
        try {
            let result = this.fs.rmdirSync(
                `${swan.env.USER_DATA_PATH}/rmdirSync`
            );
            console.log('rmdirSync success', result);
        }
        catch (err) {
            console.log('rmdirSync fail', err);
        }
    },
    saveFile() {
        this.fs.saveFile({
            tempFilePath: '/usr/temp.txt', // 僅為示例,實(shí)際上請傳真實(shí)臨時(shí)路徑地址,如 swan.downloadFile 的 tempFilePath 返回參數(shù)
            filePath: `${swan.env.USER_DATA_PATH}/`,
            success: res => {
                console.log('saveFile success', res);
            },
            fail: err => {
                console.log('saveFile fail', err);
            }
        });
    },
    saveFileSync() {
        try {
            let result = this.fs.saveFileSync(
                '/usr/temp.txt', // 僅為示例,實(shí)際上請傳真實(shí)臨時(shí)路徑地址,如 swan.downloadFile 的 tempFilePath 返回參數(shù)
                `${swan.env.USER_DATA_PATH}/`
            );
            console.log('saveFileSync success', result);
        }
        catch (err) {
            console.log('saveFileSync fail', err);
        }
    },
    stat() {
        this.fs.stat({
            path: `${swan.env.USER_DATA_PATH}/demo.txt`,
            success: res => {
                console.log('stat success', res);
            },
            fail: err => {
                console.log('stat fail', err);
            }
        });
    },
    statSync() {
        try {
            let result = this.fs.statSync(
                `${swan.env.USER_DATA_PATH}/demo.txt`
            );
            console.log('statSync success', result);
        }
        catch (err) {
            console.log('statSync fail', err);
        }
    },
    unlink() {
        this.fs.unlink({
            filePath: `${swan.env.USER_DATA_PATH}/demo.txt`,
            success: res => {
                console.log('unlink success', res);
            },
            fail: err => {
                console.log('unlink fail', err);
            }
        });
    },
    unlinkSync() {
        try {
            let result = this.fs.unlinkSync(
                `${swan.env.USER_DATA_PATH}/demo.txt`
            );
            console.log('unlinkSync success', result);
        }
        catch (err) {
            console.log('unlinkSync fail', err);
        }
    },
    unzip() {
        this.fs.unzip({
            zipFilePath: `${swan.env.USER_DATA_PATH}/demo/a.zip`,
            targetPath: `${swan.env.USER_DATA_PATH}/demo/b`,
            success: res => {
                console.log('unzip success', res);
            },
            fail: err => {
                console.log('unzip fail', err);
            }
        });
    },
    writeFile() {
        this.fs.writeFile({
            filePath: `${swan.env.USER_DATA_PATH}/demo.txt`,
            data: 'writeFile',
            success: res => {
                console.log('writeFile success', res);
            },
            fail: err => {
                console.log('writeFile fail', err);
            }
        });
    },
    writeFileSync() {
        try {
            let result = this.fs.writeFileSync(
                `${swan.env.USER_DATA_PATH}/demo.txt`,
                'writeFileSync'
            );
            console.log('writeFileSync success', result);
        }
        catch (err) {
            console.log('writeFileSync fail', err);
        }
    }


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號