Search

path provider

path provider

ํ”Œ๋Ÿฌํ„ฐ ์•ฑ์—์„œ ํŒŒ์ผ ์ €์žฅ ์œ„์น˜๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ง€์›ํ•ด์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

์ฃผ์š” ์ €์žฅ ์œ„์น˜

์ €์žฅ ์œ„์น˜
์„ค๋ช…
Temporary directory
์บ์‹œ๊ฐ™์ด ์ž„์‹œ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ณต๊ฐ„์ด๊ณ  ์–ธ์ œ๋“ ์ง€ ์‚ญ์ œ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
Documents directory
ํ•ด๋‹น ์•ฑ๋งŒ์˜ ์ €์žฅ๊ณต๊ฐ„์ด๋ฉฐ ์•ฑ์ด ์‚ญ์ œ๋˜๋ฉด ์—†์–ด์ง‘๋‹ˆ๋‹ค.
Downloads directory
๋‹ค์šด๋กœ๋“œํ•œ ํŒŒ์ผ์„ ์ €์žฅํ•˜๋Š” ๊ณต๊ฐ„์ž…๋‹ˆ๋‹ค.
Application Support directory
์•ฑ์˜ ์„ค์ •์ด๋‚˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ณต๊ฐ„์ž…๋‹ˆ๋‹ค.

์ฃผ์š” ๋ฉ”์†Œ๋“œ

๋ฉ”์†Œ๋“œ
๋ฐ˜ํ™˜๊ฐ’
์„ค๋ช…
getTemporaryDirectory()
Directory
Temporary directory๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
getApplicationDocumentsDirectory()
Directory
Documents directory๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
getDownloadsDirectory()
Directory
Downloads directory๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
getApplicationSupportDirectory()
Directory
Application Support directory๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์‹œ์ฝ”๋“œ

import 'package:path_provider/path_provider.dart'; void main() async { // Temporary directory๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. Directory temporaryDirectory = await getTemporaryDirectory(); print(temporaryDirectory.path); // /data/user/0/com.example.myapp/cache // Documents directory๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. Directory documentsDirectory = await getApplicationDocumentsDirectory(); print(documentsDirectory.path); // /data/user/0/com.example.myapp/documents // Downloads directory๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. Directory downloadsDirectory = await getDownloadsDirectory(); print(downloadsDirectory.path); // /storage/emulated/0/Download // Application Support directory๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. Directory applicationSupportDirectory = await getApplicationSupportDirectory(); print(applicationSupportDirectory.path); // /data/user/0/com.example.myapp/shared_prefs }
Dart
๋ณต์‚ฌ

์นด๋“œ ๋ฉ”๋ชจ ์•ฑ ๋งŒ๋“ค๊ธฐ

๊ฐ„๋‹จํ•œ List ๋ฐ์ดํ„ฐ๋ฅผ txt ํŒŒ์ผ๋กœ ์ €์žฅํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๊ธฐ

์ฃผ์š” ๊ธฐ๋Šฅ

โ€ข
๋ฉ”๋ชจ ์นด๋“œ ์ž‘์„ฑ: ๋ฉ”๋ชจ ์ž…๋ ฅ ํ›„, ํ”Œ๋กœํŒ… ๋ฒ„ํŠผ ํด๋ฆญ
โ€ข
๋ฉ”๋ชจ ์นด๋“œ ์‚ญ์ œ: ๋ฉ”๋ชจ ์นด๋“œ ๊ธธ๊ฒŒ ๋ˆ„๋ฅด๊ธฐ

pubspce.yaml

name: shared_data_app description: "A new Flutter project." publish_to: 'none' version: 1.0.0+1 environment: sdk: '>=3.2.3 <4.0.0' dependencies: flutter: sdk: flutter # ๊ณต์œ  ํ™˜๊ฒฝ์„ค์ • shared_preferences: ^2.2.2 # ํŒŒ์ผ path_provider: ^2.1.1 cupertino_icons: ^1.0.2 dev_dependencies: flutter_test: sdk: flutter flutter_lints: ^2.0.0 flutter: uses-material-design: true assets: - repo/
YAML
๋ณต์‚ฌ

main.dart

import 'dart:io'; import 'package:flutter/material.dart'; import 'package:path_provider/path_provider.dart'; import 'package:shared_preferences/shared_preferences.dart'; void main() { // SharedPrefences ๊ณต์œ  ํ™˜๊ฒฝ์„ค์ •์— ๋ฐ์ดํ„ฐ ์ €์žฅ ์˜ˆ์ œ runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), useMaterial3: true, ), home: const FileApp(), ); } } class FileApp extends StatefulWidget { const FileApp({super.key}); State<FileApp> createState() => _FileAppState(); } class _FileAppState extends State<FileApp> { TextEditingController _controller = TextEditingController(); List<String> itemList = []; void initState() { super.initState(); initData(); } void initData() async { List<String> savedItemList = await readListFile(); setState(() { itemList = savedItemList; }); } // ํŒŒ์ผ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ํ•จ์ˆ˜ // ๐Ÿ“• โžก List Future<List<String>> readListFile() async { List<String> itemList = []; // ์ตœ์ดˆ ํŒŒ์ผ ์ƒ์„ฑ // - ์ฒ˜์Œ ํŒŒ์ผ ์ƒ์„ฑ ์‹œ, SharedPeferences ๋กœ 'frist' ๋ผ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํ™•์ธ var key = 'first'; SharedPreferences pref = await SharedPreferences.getInstance(); var firstCheck = pref.getBool(key); var dir = await getApplicationDocumentsDirectory(); var file; bool fileExist = await File(dir.path + '/test.txt').exists(); // ์ตœ์ดˆ์ธ ๊ฒฝ์šฐ // 1. null 2. false if( firstCheck == null || firstCheck == false || fileExist == false ) { pref.setBool(key, true); // ์ตœ์ดˆ ํŒŒ์ผ ์ƒ์„ฑ // - ํ”„๋กœ์ ํŠธ ์•ˆ์˜ ํŒŒ์ผ ๊ฐ€์ ธ์˜ค๊ธฐ file = await DefaultAssetBundle.of(context).loadString('repo/test.txt'); // - ๊ฐ€์ ธ์˜จ ํŒŒ์ผ๋กœ ์Šค๋งˆํŠธํฐ์— ์ €์žฅํ•˜๊ธฐ File(dir.path + '/test.txt').writeAsStringSync(file); } else { // ํŒŒ์ผ ์ฝ๊ธฐ // File(ํŒŒ์ผ๊ฒฝ๋กœ) file = await File(dir.path + '/test.txt').readAsString(); } var array = file.split('\n'); // \n (์—”ํ„ฐ) for (var item in array) { itemList.add(item); } return itemList; } // ํŒŒ์ผ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ํ•จ์ˆ˜ void writeListFile(String data) async { // ํŒŒ์ผ ๊ฐ€์ ธ์˜ค๊ธฐ var dir = await getApplicationDocumentsDirectory(); var file = await File(dir.path + '/test.txt').readAsString(); // ๊ธฐ์กด ํŒŒ์ผ์— ์ƒˆ ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ file = file + '\n' + data; // ํŒŒ์ผ ์ €์žฅ File(dir.path + '/test.txt').writeAsStringSync(file); } // ํŒŒ์ผ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐฑ์‹ ํ•˜๋Š” ํ•จ์ˆ˜ Future<bool> deleteListFile(int index) async { // itemList ์—์„œ index ์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ ์‚ญ์ œ List<String> copyList = []; copyList.addAll(itemList); copyList.removeAt(index); // copyList ์˜ ๋ฐ์ดํ„ฐ๋“ค์„ '\n' ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ var fileData = ""; for (var item in copyList) { fileData += item + "\n"; } // ํŒŒ์ผ ์ €์žฅ try { var dir = await getApplicationDocumentsDirectory(); File(dir.path + '/test.txt').writeAsStringSync(fileData); } catch (e) { print(e); return false; } return true; } Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('File App'),), body: Container( child: Center( child: Column( children: [ // TextFied : controller, keyboardType Padding( padding: EdgeInsets.all(20.0), child: TextField( controller: _controller, keyboardType: TextInputType.text, onSubmitted: (data) { print(data); writeListFile(data); setState(() { itemList.add(data); }); _controller.text = ''; }, ), ), const SizedBox(height: 10.0,), Expanded( child: ListView.builder( itemBuilder: (context, index) { return GestureDetector( onLongPress: () async { // ๊ธธ๊ฒŒ ๋ˆ„๋ฅธ ์นด๋“œ ์‚ญ์ œ bool check = await deleteListFile(index); if( check ) { setState(() { itemList.removeAt(index); }); } }, child: Card( child: Center( child: Text( itemList[index], style: TextStyle(fontSize: 20.0), ), ), ), ); }, itemCount: itemList.length, ) ), ], ) ), ), floatingActionButton: FloatingActionButton( onPressed: () { writeListFile(_controller.text); setState(() { itemList.add(_controller.text); }); _controller.text = ''; }, child: Icon(Icons.create), ), ); } }
Dart
๋ณต์‚ฌ