diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..104f21a --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "cSpell.words": ["RNFS"] +} diff --git a/app/(tabs)/index.tsx b/app/(tabs)/index.tsx index 80db077..fc2bb4e 100644 --- a/app/(tabs)/index.tsx +++ b/app/(tabs)/index.tsx @@ -5,6 +5,7 @@ import ParallaxScrollView from '@/components/ParallaxScrollView'; import { ThemedText } from '@/components/ThemedText'; import { ThemedView } from '@/components/ThemedView'; import { Button } from 'react-native-paper'; +import FileReadWriteExample from '@/components/ui/FileReadWriteExample'; export default function HomeScreen() { return ( @@ -23,6 +24,7 @@ export default function HomeScreen() { + Step 1: Try it diff --git a/components/ui/FileReadWriteExample.tsx b/components/ui/FileReadWriteExample.tsx new file mode 100644 index 0000000..1f571d2 --- /dev/null +++ b/components/ui/FileReadWriteExample.tsx @@ -0,0 +1,51 @@ +import React, { useEffect } from 'react'; +import { View, Text, Button } from 'react-native'; +import RNFS from 'react-native-fs'; + +const FileReadWriteExample = () => { + // 定义文件路径 + const filePath = RNFS.DocumentDirectoryPath + '/example.txt'; + + // 写入文件的函数 + const writeToFile = async () => { + try { + const content = 'Hello, this is a test file content.'; + // 使用 RNFS.writeFile 方法写入文件 + await RNFS.writeFile(filePath, content, 'utf8'); + console.log('File written successfully'); + } catch (error) { + console.error('Error writing file:', error); + } + }; + + // 读取文件的函数 + const readFromFile = async () => { + try { + // 使用 RNFS.readFile 方法读取文件 + const contents = await RNFS.readFile(filePath, 'utf8'); + console.log('File content:', contents); + } catch (error) { + console.error('Error reading file:', error); + } + }; + + useEffect(() => { + // 在组件挂载时创建文件目录 + RNFS.mkdir(RNFS.DocumentDirectoryPath) + .then(() => { + console.log('Directory created successfully'); + }) + .catch((error) => { + console.error('Error creating directory:', error); + }); + }, []); + + return ( + +