uni-app设备本地文件读写——2022.11.18

封装manageFile.js

//创建注册码+设备编号文件及删除文件
const dir = '/.keyData'; //文件夹前+.是隐藏文件 避免用户误删
const pathUrl = '/.keyData/keyData.txt';
const environment = plus.android.importClass("android.os.Environment");
const BufferedReader = plus.android.importClass("java.io.BufferedReader");
const FileReader = plus.android.importClass("java.io.FileReader");
const FileWriter = plus.android.importClass("java.io.FileWriter");
const File = plus.android.importClass("java.io.File");
//文件夹根目录
const sdRoot = environment.getExternalStorageDirectory();
//写
export function writeFile(res){
	return new Promise((resolve, reject) => {
		// 方便间隔(保留源文件)
		res = JSON.stringify(res);
		try {
		    let directory = new File(sdRoot + dir);
		    if(!directory.exists()) {
				 //创建目录
		        directory.mkdirs();
		    }
		    let file = new File(sdRoot + pathUrl);
			//创建文件
		    if(!file.exists()) {
		        file.createNewFile();
		    }
			// todo 是否保留源文件内容,true保留 ,false不保留
		    let fos = new FileWriter(sdRoot + pathUrl, true);
		    fos.write(res);
		    fos.close();
		    resolve(true);
		} catch(e) {
		   reject(false);
		}
		reject(false);
	})
}
//删
export function deleteFile(){
	return new Promise((resolve, reject) => {
		let file = new File(sdRoot + pathUrl);
		if(file.exists() && file.isFile()) {
		    resolve(file.delete());
		} else {
		  reject(false);
		}
	})
}
//读
export function readFile(){
    return new Promise((resolve, reject) => {
		let readFr = new File(sdRoot + pathUrl);
		let txt = '';
		try{
			var reader = new BufferedReader(new FileReader(readFr));
			let txt = reader.readLine();
			resolve(txt);
		} catch(e){
			reject("no have the file");
		}
	})
}

函数试用

1、读文件

readFile().then(res => {
	console.log(res);	
});

2、写文件

writeFile(checkKey).then(res => {});

注意事项

代码完成后还需要管理APP权限才可在设备上读写文件
在manifest.json–app-plus–distribute中配置以下内容(可根据实际情况自定义是否在APP启动时弹出权限询问)

/* 应用发布信息 */
        "distribute" : {
            /* android打包配置 */
            "android" : {
                "permissions" : [
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    ""
                ],
                "abiFilters" : [ "armeabi-v7a" ],
                "autoSdkPermissions" : true,
                "permissionExternalStorage" : {
                    "request" : "once",
                    "prompt" : "应用保存运行状态等信息,需要获取读写手机存储(系统提示为访问设备上的照片、媒体内容和文件)权限,请允许。"
                }
            },
            /* ios打包配置 */
            "ios" : {
                "idfa" : false,
                "privacyDescription" : {
                    "NSCameraUsageDescription" : "获取相机权限拍照并上传,用于提交数据检测信息",
                    "NSLocationWhenInUseUsageDescription" : "获取您的地理位置,用于查看拍照实时地点",
                    "NSLocationAlwaysUsageDescription" : "获取您的地理位置,用于查看拍照实时地点",
                    "NSLocationAlwaysAndWhenInUseUsageDescription" : "获取您的地理位置,用于查看拍照实时地点",
                    "NSPhotoLibraryUsageDescription" : "获取相册选择图片并上传,用于提交数据检测信息",
                    "NSPhotoLibraryAddUsageDescription" : "获取相册选择图片并上传,用于提交数据检测信息"
                },
                "dSYMs" : false
            },

你可能感兴趣的:(uni-app,uni-app,前端)