uniapp と NativeJs は Android ローカル ファイルの読み取りを実装します

現在、個人テストは Android デバイスでローカル txt、json、その他のファイルを読み取るために使用できます。

1. パッケージの読み取り方法

readJsonByFile(fileNamePath) {
				let that = this;
				// 只能用于安卓 导入java类
				const File = plus.android.importClass('java.io.File')
				const BufferedReader = plus.android.importClass('java.io.BufferedReader')
				const FileReader = plus.android.importClass('java.io.FileReader')
				const FileWriter = plus.android.importClass('java.io.FileWriter')
				// 安卓11以下 /sdcard/自己的文件夹/1.txt
				// 安卓11 建议用 /storage/emulated/0/Download/自己的文件夹/1.txt
				// 读取txt文件 readFile ("/sdcard/修止符/配置.json")
				const readFile = (fileName) => {
					const readFr = new File(fileName)
					try {
						const reader = new BufferedReader(new FileReader(readFr))
						let txt
						let retxt = ''
						let flag = true
						while (flag) {
							txt = reader.readLine() // 读取文件
							if (txt == null) {
								flag = false
								break
							}
							retxt = retxt + txt
						}
						return retxt
					} catch (e) {
						console.log(e)
						return ''
					}
				}
				let json = readFile(fileNamePath);
				return eval("(" + json + ")");
			},

2. 使用する

ローカル ファイル アドレスを渡して、ファイルの内容を読み取ります。

おすすめ

転載: blog.csdn.net/start1018/article/details/128589295