在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. 福州三中集训day4

    第6天写第4天的博客….可以说是很弱了…… 讲了一天的高级数据结构,可以说很迷,先是并查集,然后是树状数组,线段树,MAP函数,KMP算法. 很难……确实不是很清楚…但是很重要,回去以后这应该说是优先 ...

  2. Typora

    Typora BB in front 如果你是一个佛(lan)系(duo),内心文艺的程序员,并且你对其他Markdown编辑器的使用效果感觉不是很好的话,可以来了解一下该软件Typora. What ...

  3. Java高级架构师(一)第16节:Mybatis动态查询和Json自动拼装

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...

  4. C#分析URL参数获取参数和值得对应列表(二)

    不错博客: [C#HttpHelper]官方产品发布与源码下载---苏飞版http://www.sufeinet.com/thread-3-1-1.html http://blog.csdn.net/ ...

  5. virtualenv、virtualenvwrapper安装和使用;Mac os的特殊性

    [sudo] pip install virtualenv 或者[sudo] pip3 install virtualenv [sudo]可用可不用 pip/pip3 install virtuale ...

  6. android 电话薄先10位匹配,若是无法匹配,则换成7位匹配

    案例 1: 假设您保存的有:A:04165191666. B:5191666.  来电号码是:04165191666   由于是7位匹配,所以A和B都能够匹配到.可是最佳匹配还是A,最后显示A: 来电 ...

  7. CMD一键获取cpu信息

    windows + R 输入cmd打开CMD 输入wmic cpu get Name 获取cpu名称-即物理cpu数 cpu get NumberOfCores获取cpu核心数 cpu get Num ...

  8. Android SVG动画PathView源代码解析与使用教程(API 14)

    使用的是一个第三方库android-pathview主要是一个自己定义View--PathView.跟全部自己定义View一样,重写了三个构造方法. 而且终于调用三个參数的构造方法,在里面获取自己定义 ...

  9. 如何让Firefox清除它记下的网站登录名和密码

  10. 【玩转cocos2d-x之三十九】Cocos2d-x 3.0截屏功能集成

    3.0的截屏和2.x的截屏基本上同样.都是利用RenderTexture来处理,在渲染之前调用call函数,然后调用Cocos的场景visit函数对其进行渲染,渲染结束后调用end函数就可以.仅仅是3 ...