import {Tester, TestCase} from '@rnoh/testerino'; import React, {useState} from 'react'; import { StyleSheet, ScrollView, View, Text, StatusBar, TextInput, Button, } from 'react-native'; import RNFS from 'react-native-fs'; import {Colors} from 'react-native/Libraries/NewAppScreen'; function FsTest(): React.JSX.Element { // 参数 const [mkdirParam, setMkdirParam] = useState(''); const [existsParams, setExistsParams] = useState(''); const [readDirExampleData, setreadDirExampleData] = useState({}); const [writeFileExampleData, setwriteFileExampleData] = useState(''); const [hashExampleData, sethashExampleData] = useState(''); const [readFileAssetsExampleData, setreadFileAssetsExampleData] = useState(''); const [existsAssetsExampleData, setexistsAssetsExampleData] = useState(''); const [existsExampleData, setexistsExampleData] = useState(''); const mkdirExample = () => { //创建文件夹 RNFS.mkdir(RNFS.DocumentDirectoryPath + '/' + mkdirParam).then( result => { console.log('file mkdirExample ' + mkdirParam); }, err => { console.error('file mkdir: ' + err.message); }, ); }; const existsExample = () => { //查找文件夹 RNFS.exists(RNFS.DocumentDirectoryPath + '/' + existsParams).then( result => { console.log('file existsExample ' + result); setexistsExampleData(JSON.stringify(result)); }, err => { console.error('file mkdir: ' + err.message); }, ); }; const readDirExample = () => { //读取文件夹 RNFS.readDir(RNFS.DocumentDirectoryPath) // On Android, use "RNFS.DocumentDirectoryPath" (MainBundlePath is not defined) .then(result => { console.log('readDirExample', result); setreadDirExampleData(result); }) .catch(err => { console.log('readDirExamplereadDirExample', err); }); }; const writeFileExample = () => { // create a path you want to write to // :warning: on iOS, you cannot write into `RNFS.MainBundlePath`, // but `RNFS.DocumentDirectoryPath` exists on both platforms and is writable var path = RNFS.DocumentDirectoryPath + '/2.txt'; // write the file RNFS.writeFile( path, ` Document 111 `, 'utf8', ) //写入文件 .then(success => { console.log( 'writeFileExample' + RNFS.DocumentDirectoryPath + '/1.html', success, ); setwriteFileExampleData(path); }) .catch(err => { console.log(err.message); }); }; const unlinkExample = () => { // create a path you want to delete var path = RNFS.DocumentDirectoryPath + '/2.txt'; RNFS.unlink(path) //删除文件 .then(() => { console.log('unlinkExample'); }) // `unlink` will throw an error, if the item to unlink does not exist .catch(err => { console.log(err.message); }); }; const [readFileParam, setReadFileParam] = useState(''); const [readFileExampleData, setreadFileExampleData] = useState({}); const readFileExample = () => { RNFS.readFile(RNFS.DocumentDirectoryPath + `/${readFileParam}`) .then(contents => { console.log('readFileExample', contents.length); setreadFileExampleData(contents); }) .catch(err => { console.log('ygb 读取失败:', err.message, err.code); }); }; const appendFileExample = () => { //给文件写入内容 var path = RNFS.DocumentDirectoryPath + '/2.txt'; RNFS.appendFile(path, '新添加的文本', 'utf8') .then(success => { console.log('appendFileExamplesuccess'); }) .catch(err => { console.log(err.message); }); }; const copyFileExample = () => { const path = RNFS.DocumentDirectoryPath + '/2.txt'; const path1 = RNFS.DocumentDirectoryPath + '/3.txt'; RNFS.copyFile(path, path1) .then(result => { console.log('copyFileExample', path, path1); }) .catch(err => { console.log(err.message); }); }; const moveFileExample = () => { const path = RNFS.DocumentDirectoryPath + '/3.txt'; const path1 = RNFS.DocumentDirectoryPath + '/eee/3.txt'; RNFS.moveFile(path, path1) .then(result => { console.log('moveFileExample', path, path1); }) .catch(err => { console.log(err.message); }); }; const [readExampleData, setreadExampleData] = useState({}); const readExample = () => { const path = RNFS.DocumentDirectoryPath + '/2.txt'; RNFS.read(path, 1024, 0, 'utf8') .then(result => { console.log('readExample', result); setreadExampleData(result); }) .catch(err => { console.log(err.message); }); }; const [starExampleData, setstarExampleData] = useState({}); const starExample = () => { const path = RNFS.DocumentDirectoryPath + '/2.txt'; RNFS.stat(path) .then(result => { console.log('starExample', result); setstarExampleData(result); }) .catch(err => { console.log(err.message); }); }; const downloadFileExample = () => { RNFS.downloadFile({ fromUrl: 'https://www-file.huawei.com/minisite/media/annual_report/annual_report_2022_cn.pdf', // URL to download file from toFile: RNFS.DocumentDirectoryPath + '/3.pdf', }); }; const readFileAssetsExample = () => { //用户获取resources/rawfile/1.txt 目录下对应的rawfile文件内容,使用callback形式返回字节数组。 RNFS.readFileAssets('1.txt', 'utf8') .then(res => { console.log('readFileAssetsExample', res); setreadFileAssetsExampleData(res); }) .catch(err => { console.log('readFileAssetsExample error', err); }); }; const existsAssetsExample = () => { //用户获取resources/rawfile/ 底下有没有assets RNFS.existsAssets('assets') .then(res => { console.log('existsAssetsExample', res); setexistsAssetsExampleData(JSON.stringify(res)); }) .catch(err => { console.log('existsAssetsExample error', err); }); }; const hashExample = () => { //用户获取resources/rawfile/ 底下有没有assets const path = RNFS.DocumentDirectoryPath + '/2.txt'; RNFS.hash(path, 'md5') .then(res => { console.log('hash', res); sethashExampleData(res); }) .catch(err => { console.log('hash error', err); }); }; const touchExample = () => { //用户获取resources/rawfile/ 底下有没有assets const path = RNFS.DocumentDirectoryPath + '/2.txt'; RNFS.touch(path, new Date('2018-12-2')) .then(res => { console.log('touchExampletouchExampletouchExample', res); }) .catch(err => { console.log('hash error', err); }); }; const writeExample = () => { //用户获取resources/rawfile/ 底下有没有assets const path = RNFS.DocumentDirectoryPath + '/3.txt'; RNFS.write(path, 'utxxxxxx') .then(res => { console.log('touchExampletouchExampletouchExample', res); }) .catch(err => { console.log('hash error', err); }); }; return ( <> { return ( 当前库为文件操作库 不存在页面效果请关注文件系统变更{'\n'} 相关资料链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/app-sandbox-directory-0000001774280086 FS.DocumentDirectoryPath:{'\n'} {RNFS.DocumentDirectoryPath} FS.CachesDirectoryPath:{RNFS.CachesDirectoryPath} FS.MainBundlePath:{'\n'} {RNFS.MainBundlePath} FS.TemporaryDirectoryPath:{'\n'} {RNFS.TemporaryDirectoryPath} FS.LibraryDirectoryPath:{'\n'} {RNFS.LibraryDirectoryPath} ); }} assert={async ({expect, state}) => { expect(state).to.be.true; }} /> { return ( setMkdirParam(mkdirParam)} placeholderTextColor="#9a73ef" autoCapitalize="none" /> {' '} {RNFS.DocumentDirectoryPath + '/' + mkdirParam}