flutter 读写文件
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:async';
import 'dart:io'; void main() {
runApp(
MaterialApp(
title: 'Read/Write Files',
home: MyApp(storage: TextStorage()),
),
);
} class TextStorage {
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
} Future<File> get _localFile async {
final path = await _localPath;
return File('$path/text.txt');
} Future<String> readFile() async {
try {
final file = await _localFile; String content = await file.readAsString();
return content;
} catch (e) {
return '';
}
} Future<File> writeFile(String text) async {
final file = await _localFile;
return file.writeAsString('$text\r\n', mode: FileMode.append);
} Future<File> cleanFile() async {
final file = await _localFile;
return file.writeAsString('');
}
} class MyApp extends StatefulWidget {
final TextStorage storage; MyApp({Key key, @required this.storage}) : super(key: key); @override
_MyAppState createState() => _MyAppState();
} class _MyAppState extends State<MyApp> {
TextEditingController _textField = new TextEditingController(); String _content = ''; @override
void initState() {
super.initState();
widget.storage.readFile().then((String text) {
setState(() {
_content = text;
});
});
} Future<File> _writeStringToTextFile(String text) async {
setState(() {
_content += text + '\r\n';
}); return widget.storage.writeFile(text);
} Future<File> _clearContentsInTextFile() async {
setState(() {
_content = '';
}); return widget.storage.cleanFile();
} @override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Read/Write File Example'),
backgroundColor: Colors.blue,
),
body: Container(
padding: EdgeInsets.all(20.0),
child: Column(
children: <Widget>[
TextField(
controller: _textField,
),
Padding(
padding: EdgeInsets.all(20.0),
child: RaisedButton(
child: Text('Write to File'),
onPressed: () {
if (_textField.text.isNotEmpty) {
_writeStringToTextFile(_textField.text);
_textField.clear();
}
},
),
),
Padding(
padding: EdgeInsets.only(bottom: 20.0),
child: RaisedButton(
child: Text(
'Clear Contents',
style: TextStyle(color: Colors.white),
),
color: Colors.redAccent,
onPressed: () {
_clearContentsInTextFile();
},
),
),
Expanded(
flex: 1,
child: new SingleChildScrollView(
child: Text(
'$_content',
style: TextStyle(
color: Colors.blue,
fontSize: 22.0,
),
),
),
),
],
),
),
);
}
}
记得家权限.
flutter 读写文件的更多相关文章
- Hyper-V无法文件拖拽解决方案~~~这次用一个取巧的方法架设一个FTP来访问某个磁盘,并方便的读写文件
异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 服务器相关的知识点:http://www.cnblogs.com/dunitia ...
- 计算机程序的思维逻辑 (60) - 随机读写文件及其应用 - 实现一个简单的KV数据库
57节介绍了字节流, 58节介绍了字符流,它们都是以流的方式读写文件,流的方式有几个限制: 要么读,要么写,不能同时读和写 不能随机读写,只能从头读到尾,且不能重复读,虽然通过缓冲可以实现部分重读,但 ...
- Python读写文件
Python读写文件1.open使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. file_object = open('t ...
- php中并发读写文件冲突的解决方案
在这里提供4种高并发读写文件的方案,各有优点,可以根据自己的情况解决php并发读写文件冲突的问题. 对于日IP不高或者说并发数不是很大的应用,一般不用考虑这些!用一般的文件操作方法完全没有问题.但如果 ...
- C#读写文件的方法汇总_C#教程_脚本之家
C#读写文件的方法汇总_C#教程_脚本之家 http://www.jb51.net/article/34936.htm
- Inno Setup 如何读写文件
软件安装的实质就是拷贝,对于简单的打包当然不需要考虑修改某(配置)文件.通过inno修改文件的目的在于把安装时相关信息写入文件中,提供其它应用的读取,而这些信息也只能在安装时才能确定,比如安装用户选择 ...
- java使用IO读写文件总结
每次用到IO的读写文件都老忘记写法,都要翻过往笔记,今天总结下,省的以后老忘.java读写文件的IO流分两大类,字节流和字符流,基类分别是字符:Reader和Writer:字节:InputStream ...
- python读写文件时中文的转码问题
读写文件都要将中文转为unicode字符. 读文件: u = unicode(s, 'gbk') 这里不能使用encode 写文件: u = encode('utf')
- JAVA基础学习之流的简述及演示案例、用缓冲区方法buffer读写文件、File类对象的使用、Serializable标记接口(6)
1.流的简述及演示案例输入流和输出流相对于内存设备而言.将外设中的数据读取到内存中:输入将内存的数写入到外设中:输出.字符流的由来:其实就是:字节流读取文字字节数据后,不直接操作而是先查指定的编码表. ...
随机推荐
- Python练手例子(5)
25.求1+2!+3!+...+20!的和. 程序分析:此程序只是把累加变成了累乘. #python3.7 n = 0 s = 0 t = 1 for n in range(1, 21): t *= ...
- Python: map() and reduce()
map()函数接收两个参数,一个是函数,一个是序列,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回. 举例说明,比如我们有一个函数f(x)=x2,要把这个函数作用在一个lis ...
- fiddler修改Requests之前的数据和response 之后的数据
1. 开启抓包 file--->capture traffic 2. 在页面底部黑框输入bpu http://www.runoob.com/?s=mysql 3. 在浏览器URL输入http:/ ...
- Java笔记--引用类型的使用
使用引用类型的一般步骤: 1.导包:指定需要使用的目标在什么位置,在publicclass之前一行写代码 import 包名路径 2.创建:通常需要创建之才能使用,格式: 数据类型 变量名称 = ne ...
- Java多线中基础知识整理
Java多线程中相关术语: 一.Java多线程原则 1.原子性:一个操作或者多个操作要么全部执行并且执行的过程不会被任何因素打断,要么就都不执行.一般使用线程同步或者Lock锁来确保. 2.可见性(J ...
- ACM练习中关于LCS的题目
You are planning to take some rest and to go out on vacation, but you really don’t know which cities ...
- 内网gitlab11.2升级至11.4.5
当前gitlab版本 宿主机是一台ubuntu 运行备份命令 sudo gitlab-rake gitlab:backup:create STRATEGY=copy 升级命令 sudo apt-get ...
- 快速排序 之添加复合插入排序和原始序列取中值左pivot
quicksort中,当n小于一定值时,排序效率就比直接插入排序底了,所以,此时就不要再递归下去了,直接插入排序好了:快速的原理就是因为折半递归,所以初始pivot应该有个好一点的选择,这里在原序列左 ...
- face++静态库转为动态库之二
上一篇的时候,已经介绍了如何将carthage转为动态库.这一篇,我们是单纯的建一个动态库.还是以face++为例 查看上一篇: face++静态库转为动态库 制作动态库 1.创建一个工程MGLive ...
- EF性能检测工具MiniProfilerEF6的使用
一.在VS项目中分别安装包MiniProfiler.MiniProfiler.EF6.MiniProfiler.MVC4 二.在Global.asax文件的Application_BeginReque ...