Unity在Android端PC端以及编辑器环境下读取并写入文本的操作,直接上代码

创建并写入文本内容
     //创建并写入     fileName为文件名称(记得加后缀)    content为需要写入的文本内容public void CreateFileInPersistentData(string fileName,string content){string path = Application.persistentDataPath + "/" + fileName;if (File.Exists(path)){return;}else{FileStream fs = new FileStream(path, FileMode.Create);           fs.Close();WriteTXT(fileName, content);            }}
写入文本内容
 public void WriteTXT(string fileName, string s){string url = "";if (Application.platform == RuntimePlatform.Android){url = Application.persistentDataPath + "/" + fileName;File.WriteAllText(url, s);}else if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer){url = Application.persistentDataPath + "/" + fileName;FileStream fs = new FileStream(url, FileMode.Open);byte[] bytes = new UTF8Encoding().GetBytes(s.ToString());fs.Write(bytes, 0, bytes.Length);fs.Close();}}
读取文本内容
 public string LoadTXTtoString(string fileName){string url = Application.persistentDataPath + "/" + fileName;string s = "";if (Application.platform == RuntimePlatform.Android){s = File.ReadAllText(url);}else if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer){FileStream fs = new FileStream(url, FileMode.Open);byte[] bytes = new byte[100];fs.Read(bytes, 0, bytes.Length);//将读取到的二进制转换成字符串s = new UTF8Encoding().GetString(bytes);fs.Close();}return s;}
调用方法演示
    public Text show_text;public Button ok_btn;private void Awake(){CreateFileInPersistentData("test.txt", "这是一个测试文本...");        }private void Start(){ok_btn.onClick.AddListener(() => {show_text.text = LoadTXTtoString("test.txt"});}

Unity | 安卓 读取和写入TXT文本操作相关推荐

  1. python 利用os库 读取、写入txt文本

    目录 读写模式 1.读取txt文本 1.1read() 一次性读全部内容 1.2readline()    读取第一行内容 1.3readlines()     列表 1.3.1直接输出 1.3.2f ...

  2. C语言读取和写入txt文本

    #include<stdio.h> #include<stdlib.h> #define MAX 1000 int main() {//打开文件FILE *r=fopen(&q ...

  3. python 写txt 换行_写入txt文本的内容为什么没换行效果?

    PHP把内容写入文件,并每次自动换行 weixin_4089680029712018-07-20 Java文本追加.换行.输出到TXT u01422837514692015-12-15 Android ...

  4. 【新星计划】 Python的txt文本操作-读、写

    Python的txt文本操作-读.写 读取txt文本 python常用的读取文件函数有三种read().readline().readlines() 以读取上述txt为例,看一下三者的区别 read( ...

  5. java 导入导出txt文件_Java读取和写入txt文件

    1 问题描述 对于java的读取和写入txt一直心存疑惑,随着知识的积累,又重新进行学习,对java的文件读写理解更加深刻,在这里将自己的小小经验总结分享给大家.下面是大家了解java流的一个基本框架 ...

  6. python读取txt文件写入-python 读取、写入txt文件的示例

    写入文件 使用open()函数和write()函数 但是有两种写法,分别是'a'和'w' 'a' 表示写入文件 若无该文件会直接创建一个 如果存在这个文件,会接着已有的内容的后面写入 with ope ...

  7. c++读取和写入TXT文件的整理

    c++读取和写入TXT文件的整理 #include "stdafx.h" #include <iostream> //无论读写都要包含<fstream>头文 ...

  8. c++读取和写入TXT文件的整理 1

    c++读取和写入TXT文件的整理 #include "stdafx.h" #include <iostream> //无论读写都要包含<fstream>头文 ...

  9. java properties更新_对Java配置文件Properties的读取、写入与更新操作

    String filepath=System.getProperty("user.dir"); 对下面的程序很有用... /** * 实现对Java配置文件Properties的读 ...

  10. java配置文件强制更新_对Java配置文件Properties的读取、写入与更新操作

    对Java配置文件Properties的读取.写入与更新操作 注:当前项目路径是 String filepath=System.getProperty("user.dir"); 对 ...

最新文章

  1. HTTPS安全证书介绍
  2. msicuu.exe (msizap.exe),程序的作用
  3. 《数据库原理与应用》(第三版)第13章 安全管理 基础 习题参考答案
  4. android StringBuilder的Capacity的使用
  5. 常用函数式接口之Predicate
  6. javascript中的命名规则和方法
  7. CF16A Flag
  8. java联合主键_hibernate联合主键映射(注解版)
  9. centos 中查找文件、目录、内容
  10. swift项目 9.3以前版本模拟器运行出错
  11. lisp画垫圈_晓东CAD家园-论坛-LISP/VLISP程序库-[LISP程序]:俺的画内六角圆柱头螺钉的LISP程序-见附件 - Powered by Discuz!...
  12. java concurrent int_Java高级特性系列--Concurrent
  13. python写二进制大文件,如何将文件写入二进制文件,或在大文件中编辑单行– Python...
  14. 毕设全记录——探索 JavaBDD
  15. (翻译)机器学习:E.coli数据集的不平衡多类分类
  16. 网上订餐叫外卖的发展优势
  17. 生于安乐,死于忧患.
  18. 怎么免卸载升级太极中的软件
  19. pip报错:No module named pip
  20. 横冲直撞html5,6系横冲直撞 5系列最后的疯狂

热门文章

  1. android webview输入框_Android WebView 软键盘挡住输入框
  2. B站左程云算法视频高级班02
  3. 图解PCIE原理(从软件角度)
  4. QT_qss文件简易使用教程
  5. 问题:找不到该项目,该项目不在指定目录下.......请确认位置......
  6. Boosting原理学习
  7. DevOps ACA 软件代码与质量管理(六)
  8. SuiteCRM的汉化
  9. 自主研发的车牌识别SDK
  10. UiPath PDF操作