在Assets下新建文件夹StreamingAssets。然后下面代码可在其中生成test.txt文件,并读写:

using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;
public class readAndWriteFile : MonoBehaviour {

void Start(){
        test ();

}
    void test(){
    
        //write file
        {
            List<string> strListToWrite = new List<string> ();
            strListToWrite.Add ("hellow 1");
            strListToWrite.Add ("lady 4");
            strListToWrite.Add ("i love  you   5");
            writeToFile ("test.txt", false, strListToWrite);
        }
        //read file
        List<List<string> > strMat=null;
        {
            strMat=readFromFileToStrMat("test.txt");
        }
        //print reading result
        {
            printStrMat(strMat);
        }
    }
    public void writeToFile(string fileNameWithExt,bool isAppend,List<string> strList){
        string path=Application.streamingAssetsPath+"/"+fileNameWithExt;
        Debug.Log (path);
        StreamWriter sw = new StreamWriter(path,isAppend);
        int strCount = strList.Count;
        for (int i=0; i<strCount; i++) {
            sw.WriteLine (strList[i]);
        }
        sw.Close ();
    }
    public List<string> readFromFileToStrList(string fileNameWithExt){
        List<string> strList = new List<string> ();
        StreamReader sr = new StreamReader (Application.streamingAssetsPath+"/"+fileNameWithExt);
        string line = sr.ReadLine ();
        while (line!=null) {
            strList.Add(line);
            line = sr.ReadLine ();
        }
        sr.Close ();
        return strList;

}
    public List<List<string> > readFromFileToStrMat(string fileNameWithExt){
        List<string> strList = readFromFileToStrList (fileNameWithExt);
        List<List<string> > strMat = strListToSubStrMat (strList);
        return strMat;
    }
    public void printStrList(List<string> strList){
        int strCount = strList.Count;
        for (int i=0; i<strCount; i++) {
            print(strList[i]);
        }

}
    public void printStrMat(List<List<string> > strMat){
        int nRow = strMat.Count;
        for (int i=0; i<nRow; i++) {
            print ("row"+i+":");
            int nCol=strMat[i].Count;
            for(int j=0;j<nCol;j++){
                print (strMat[i][j]);
            }
        }
    
    }
    public List<string> strToSubStrList(string str){
        string[] subStrArr=str.Split (new char[]{' ','\n'});
        List<string> subStrList = new List<string> ();
        int subStrCount = subStrArr.Length;
        for (int i=0; i<subStrCount; i++) {
            string subStr=subStrArr[i];
            if(subStr.Length!=0){
                subStrList.Add(subStrArr[i]);
            }
        }
        return subStrList;
    }
    public List<List<string> > strListToSubStrMat(List<string> strList){
        List<List<string> > subStrMat=new List<List<string> >();
        int strCount = strList.Count;
        for (int i=0; i<strCount; i++) {
            List<string> subStrList=strToSubStrList(strList[i]);
            subStrMat.Add(subStrList);
        }//got subStrMat
        return subStrMat;

}
}

另外一种读取txt文件的方法:http://www.cnblogs.com/wantnon/p/4605415.html

unity, write/read txt file的更多相关文章

  1. Java read txt file

    package com.Yang; import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;i ...

  2. Split CSV/TXT file

    void Main(){ var path = @"c:\sourceGit\speciesLatLon.txt"; var inputLines = File.ReadAllLi ...

  3. Save matrix to a txt file - matlab 在matlab中将矩阵变量保存为txt格式

    Source: Baidu Wenku % Original code has been modified dirMain = 'D:\test\'; fid = fopen([dirMain, 't ...

  4. VS Extension: Create a txt file and set the content

    使用 Visual Studio Extension 创建一个文本文件,并填入内容. 需要引用 EnvDTE C:\Program Files (x86)\Microsoft Visual Studi ...

  5. [JS] save txt file

    (function () { var blob = new Blob(['content'], {type: 'text/plain; charset=utf-8'}), blobUrl = URL. ...

  6. Big Txt File(一)

    对于当今的数据集来说,动不动就上G的大小,市面的软件大多不支持,所以需要自己写一个. 常见的txt文本行形式存储的时候也不过是行数多些而已,可以考虑只观测部分行的方式,基于这个思路可以搞一个大数据的浏 ...

  7. python read txt file

    refer to: http://www.jianshu.com/p/d8168034917c http://www.liaoxuefeng.com/wiki/001374738125095c955c ...

  8. 【软连接已存在,如何覆盖】ln: failed to create symbolic link ‘file.txt’: File exists

    ln -s 改成 ln -sf f在很多软件的参数中意味着force ln -sf /usr/bin/bazel-1.0.0 /usr/bin/bazel

  9. Java基础面试操作题: File IO 文件过滤器FileFilter 练习 把一个文件夹下的.java文件复制到另一个文件夹下的.txt文件

    package com.swift; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File ...

随机推荐

  1. zoj3988

    zoj3988 题意 如果一个集合 \(\{i,j\}\) 满足 \(i\neq j\) 且 \(a[i]+a[j]\) 是素数,则称之为素数集合. 给出一些数字,这些数字可以组成多种素数集合,从这些 ...

  2. 并查集【p1197】[JSOI2008]星球大战

    Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治着整个星系. 某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通 ...

  3. 1.3(Spring学习笔记)Spring-AOP

    一.AOP简介 AOP面向切面编程,是将一种横向抽取机制,将多个类中需要使用的方法提取出来. 例如,这里有两个类,一个Cat,一个Dog,动物都需要吃饭睡觉,如果按照传统的思想. 给两类中都添加吃饭和 ...

  4. iOS UILabel自定义行间距

    NSString *hintStr = @"输入材料标题搜索材料\n注:可根据材料序号直接搜索, 如TPO23"; CGSize size = [toolset returnTex ...

  5. Jackson使用ObjectManage#readValue传入泛型T的问题

    说明:没找到合适的方法,持续关注这个问题 参考: https://stackoverflow.com/questions/11664894/jackson-deserialize-using-gene ...

  6. microsoft visual studio遇到了问题,需要关闭

    http://www.microsoft.com/zh-cn/download/confirmation.aspx?id=13821 装上这个补丁: WindowsXP-KB971513-x86-CH ...

  7. JS面向对象函数的四种调用模式

    函数的四种调用模式 概念 在 js 中,无论是函数, 还是方法, 还是事件, 还是构造器,...这些东西的本质都是函数 函数, 方法, 事件, 构造器,...只是所处的位置不同 这四种模式分别是 函数 ...

  8. notepad++ 常用的插件及教程

    NotePad++ 教程 HEX-Editor      http://files.cnblogs.com/pengdonglin137/HexEditor_0_9_5_UNI_dll.zip 我的N ...

  9. 如何查看oracle的sid

      1.怎样查看Oracle的数据库名称sid 用sysdba身份登录 比如 conn sys/密码 as sysdba 匿名管理员登陆 执行 select name form V$database; ...

  10. mac上虚拟机:VMWare Fusion, VirtualBox, Parallels Desktop, CrossOver, Veertu

    作者:Louis Tong链接:https://www.zhihu.com/question/35731328/answer/66127970来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非 ...