3.1 创建文件夹
_createDir() async {
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = ‘ d o c u m e n t s D i r e c t o r y . p a t h {documentsDirectory.path} documentsDirectory.path{Platform.pathSeparator}dirName’;
var dir = Directory(path);
var exist = dir.existsSync();
if (exist) {
print(‘当前文件夹已经存在’);
} else {
var result = await dir.create();
print(‘$result’);
}
}
说明:
-
Platform.pathSeparator表示路径分隔符,对于Android和iOS来说表示‘/’
-
create 中有一个可选参数 recursive ,默认值为 false,false 表示只能创建最后一级文件夹
-
如果创建 “dir1/dir2” 这种嵌套文件夹,recursive为 false 时将抛出异常,设置为 true 可以创建嵌套文件夹
3.2 遍历文件夹下文件
_dirList() async {
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = ‘ d o c u m e n t s D i r e c t o r y . p a t h {documentsDirectory.path} documentsDirectory.path{Platform.pathSeparator}dirName’;
Stream fileList = Directory(path).list();
await for(FileSystemEntity fileSystemEntity in fileList){
print(‘$fileSystemEntity’);
}
}
说明:
-
Directory(path).list()中有一个可选参数recursive,默认值为false,表示只遍历当前目录;
-
设置为true时表示遍历当前目录及子目录。
判断文件的类型:
await for(FileSystemEntity fileSystemEntity in fileList){
print(‘$fileSystemEntity’);
FileSystemEntityType type = FileSystemEntity.typeSync(fileSystemEntity.path);
}
文件的类型:
-
file:文件
-
directory:文件夹
-
link:链接文件
-
notFound:未知
3.3 重命名文件夹名称
_dirRename() async{
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = ‘ d o c u m e n t s D i r e c t o r y . p a t h {documentsDirectory.path} documentsDirectory.path{Platform.pathSeparator}dirName’;
var dir = Directory(path);
var dir3= await dir.rename(‘ d i r . p a r e n t . a b s o l u t e . p a t h {dir.parent.absolute.path} dir.parent.absolute.path{Platform.pathSeparator}dir3’);
}
3.4 删除文件夹
_deleteDir() async {
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = ‘ d o c u m e n t s D i r e c t o r y . p a t h {documentsDirectory.path} documentsDirectory.path{Platform.pathSeparator}dir3’;
var dir = await Directory(path).delete();
}
说明:
-
delete中有一个可选参数recursive,默认值为false,为false时如果删除的文件夹下还有内容将无法删除,抛出异常
-
设置为true时,删除当前文件夹及文件夹下所有内容
4.1 创建一个 file.txt 文件
_createFile() async {
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = ‘ d o c u m e n t s D i r e c t o r y . p a t h {documentsDirectory.path} documentsDirectory.path{Platform.pathSeparator}dirName${Platform.pathSeparator}file.txt’;
var file = await File(path).create(recursive: true);
}
说明:
-
create 中有一个可选参数 recursive,默认值为 false,为 false 时只创建文件,文件夹路径不存在抛出异常
-
设置为 true 时,创建文件及不存在的路径文件夹
4.2 写入数据
_write2File() async{
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = ‘ d o c u m e n t s D i r e c t o r y . p a t h {documentsDirectory.path} documentsDirectory.path{Platform.pathSeparator}dirName${Platform.pathSeparator}file.txt’;
var file=File(path);
if (file.existsSync()) {
file.writeAsString(‘写入数据文件’); //写入字符串
//file.writeAsBytes(Utf8Encoder().convert(“写入数据文件”));//写入 bytes 数据
//file.openWrite(mode: FileMode.append).write(‘追加到末尾’); //向末尾追加内容
}
}
4.3 读取数据
_readFile() async{
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = ‘ d o c u m e n t s D i r e c t o r y . p a t h {documentsDirectory.path} documentsDirectory.path{Platform.pathSeparator}dirName${Platform.pathSeparator}file.txt’;
var file=File(path);
if (file.existsSync()) {
List lines = await file.readAsLines();
lines.forEach((element) {
print(‘$element’);
});
}
}
4.4 删除文件
_deleteFile() async{
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = ‘ d o c u m e n t s D i r e c t o r y . p a t h {documentsDirectory.path} documentsDirectory.path{Platform.pathSeparator}dirName${Platform.pathSeparator}file.txt’;
var file=File(path);
if (file.existsSync()) {
file.delete();
}
}
5.1 添加json文件数据
读取项目中文件,比如 asset/json/data.json 文件,data.json 文件中为 json 格式数据
[
{
“desc”: “开发环境搭建。”,
“title”: “第一章”
},
{
“desc”: “语法知识学习”,
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
尾声
评论里面有些同学有疑问关于如何学习material design控件,我的建议是去GitHub搜,有很多同行给的例子,这些栗子足够入门。
有朋友说要是动真格的话,需要NDK以及JVM等的知识,首现**NDK并不是神秘的东西,**你跟着官方的步骤走一遍就知道什么回事了,无非就是一些代码格式以及原生/JAVA内存交互,进阶一点的有原生/JAVA线程交互,线程交互确实有点蛋疼,但平常避免用就好了,再说对于初学者来说关心NDK干嘛,据鄙人以前的经历,只在音视频通信和一个嵌入式信号处理(离线)的两个项目中用过,嵌入式信号处理是JAVA->NDK->.SO->MATLAB这样调用的我原来MATLAB的代码,其他的大多就用在游戏上了吧,一般的互联网公司会有人给你公司的SO包的。
至于JVM,该掌握的那部分,相信我,你会掌握的,不该你掌握的,有那些专门研究JVM的人来做,不如省省心有空看看计算机系统,编译原理。
一句话,平常多写多练,这是最基本的程序员的素质,尽量挤时间,读理论基础书籍,JVM不是未来30年唯一的虚拟机,JAVA也不一定再风靡未来30年工业界,其他的系统和语言也会雨后春笋冒出来,但你理论扎实会让你很快理解学会一个语言或者框架,你平常写的多会让你很快熟练的将新学的东西应用到实际中。
初学者,一句话,多练。
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
一般的互联网公司会有人给你公司的SO包的。**
至于JVM,该掌握的那部分,相信我,你会掌握的,不该你掌握的,有那些专门研究JVM的人来做,不如省省心有空看看计算机系统,编译原理。
一句话,平常多写多练,这是最基本的程序员的素质,尽量挤时间,读理论基础书籍,JVM不是未来30年唯一的虚拟机,JAVA也不一定再风靡未来30年工业界,其他的系统和语言也会雨后春笋冒出来,但你理论扎实会让你很快理解学会一个语言或者框架,你平常写的多会让你很快熟练的将新学的东西应用到实际中。
初学者,一句话,多练。
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!