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 读写文件的更多相关文章

  1. Hyper-V无法文件拖拽解决方案~~~这次用一个取巧的方法架设一个FTP来访问某个磁盘,并方便的读写文件

    异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 服务器相关的知识点:http://www.cnblogs.com/dunitia ...

  2. 计算机程序的思维逻辑 (60) - 随机读写文件及其应用 - 实现一个简单的KV数据库

    57节介绍了字节流, 58节介绍了字符流,它们都是以流的方式读写文件,流的方式有几个限制: 要么读,要么写,不能同时读和写 不能随机读写,只能从头读到尾,且不能重复读,虽然通过缓冲可以实现部分重读,但 ...

  3. Python读写文件

    Python读写文件1.open使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. file_object = open('t ...

  4. php中并发读写文件冲突的解决方案

    在这里提供4种高并发读写文件的方案,各有优点,可以根据自己的情况解决php并发读写文件冲突的问题. 对于日IP不高或者说并发数不是很大的应用,一般不用考虑这些!用一般的文件操作方法完全没有问题.但如果 ...

  5. C#读写文件的方法汇总_C#教程_脚本之家

    C#读写文件的方法汇总_C#教程_脚本之家 http://www.jb51.net/article/34936.htm

  6. Inno Setup 如何读写文件

    软件安装的实质就是拷贝,对于简单的打包当然不需要考虑修改某(配置)文件.通过inno修改文件的目的在于把安装时相关信息写入文件中,提供其它应用的读取,而这些信息也只能在安装时才能确定,比如安装用户选择 ...

  7. java使用IO读写文件总结

    每次用到IO的读写文件都老忘记写法,都要翻过往笔记,今天总结下,省的以后老忘.java读写文件的IO流分两大类,字节流和字符流,基类分别是字符:Reader和Writer:字节:InputStream ...

  8. python读写文件时中文的转码问题

    读写文件都要将中文转为unicode字符. 读文件: u = unicode(s, 'gbk') 这里不能使用encode 写文件: u = encode('utf')

  9. JAVA基础学习之流的简述及演示案例、用缓冲区方法buffer读写文件、File类对象的使用、Serializable标记接口(6)

    1.流的简述及演示案例输入流和输出流相对于内存设备而言.将外设中的数据读取到内存中:输入将内存的数写入到外设中:输出.字符流的由来:其实就是:字节流读取文字字节数据后,不直接操作而是先查指定的编码表. ...

随机推荐

  1. Ehcache 3.7文档—基础篇—XML Configuration

    你可以使用xml配置创建CacheManager,根据这个schema definition ( http://www.ehcache.org/documentation/3.7/xsds.html# ...

  2. DjangoRestFramework学习三之认证组件、权限组件、频率组件、url注册器、响应器、分页组件

    DjangoRestFramework学习三之认证组件.权限组件.频率组件.url注册器.响应器.分页组件   本节目录 一 认证组件 二 权限组件 三 频率组件 四 URL注册器 五 响应器 六 分 ...

  3. String工具类2

    1:比较字符串 public static void main(String[] args) { // String去创建对象有多种方式 // 方式1 直接字面值赋值 String s = " ...

  4. 基于hortonworks的大数据集群环境部署流水

    一.ambari及HDP安装文件下载地址: 1.系统 操作系统:CentOS7 2.软件 本次安装采用最新版本: ambari-2.7.0.0 hdp-3.0.0.0 详细信息及下载地址如下: Amb ...

  5. Multi-Projector Based Display Code ---- Calibration

    Overview As mentioned previously, there are two main steps in generating a seamless display. The fir ...

  6. poj1699

    #include<iostream> #include<cstring> using namespace std; ][]; ],len[],addlen[][]; int m ...

  7. Docker 实战(二)——centos7镜像安装nginx,将安装nginx的centos容器生成新的镜像,并导出

    Docker centos7镜像安装nginx 1.安装docker 使用yum安装docker不再重复:见  Linux常用命令 2.pull centos 1)在docker仓库中搜索centos ...

  8. 解决spring 用@Value注入配置时候出现中文乱码问题

    只要是乱码,很明显需要指定编码格式,为utf-8 <!-- 注解使用properties --> <bean id="configProperties" clas ...

  9. Redis之基本使用

    基本介绍 Redis是一种key-value存储形式的非关系型数据库,也是一个强大的内存型存储系统,但是它比传统的Memcached 更灵活,支持更多的数据类型,同时也可以持久化. 支持的数据类型 先 ...

  10. table-一列细分为多列(合并单元格)

    这个是一个很常见的一个表格展示需求,其中最要的就两个属性 rowspan 和 colspan .他们分别就是合并行 与 合并列. 要做的效果是如图下面这个,联系电话就是合并了单元格.这个说法类似于ex ...