在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. Codeforces #436 Div2 E

    #436 Div2 E 题意 某人的房子着火了,现在有 \(n\) 件物品待抢救,每件物品有抢救需要的时间和自身的价值,以及过多长时间物品会损坏.问最多一共可以抢救价值多少的物品? 分析 看数据就知道 ...

  2. Milk Pails(BFS)

    Milk Pails 时间限制: 1 Sec  内存限制: 64 MB提交: 16  解决: 4[提交][状态][讨论版] 题目描述 Farmer John has received an order ...

  3. 【博弈论】【SG函数】Codeforces Round #188 (Div. 1) D. Game with Powers

    将整个游戏可以划分成若干个互不相交的子游戏. 每个子游戏的sg值只与其中的数的个数有关.而这个数不会超过30. 于是可以预处理出这个sg值表. 然后从1到n枚举,对<=sqrt(n)的部分,用个 ...

  4. 【Treap模板详细注释】BZOJ3224-普通平衡树

    模板题:D错因见注释 #include<iostream> #include<cstdio> #include<cstring> #include<algor ...

  5. JNI概述

    JNI是Java Native Interface的缩写,它提供了若干的API实现了Java和其他语言的通信(主要是C&C++). JNI 让你在利用强大 Java 平台的同时,使你仍然可以用 ...

  6. SonarQube分析报告无法上传的问题

    '); 由于SonarQube5.6 api/ce/submit 接口报以下异常,导致jenkins构建结果显示为失败~: Caused by: java.lang.NullPointerExcept ...

  7. 关于BOM UTF8

    这三篇可以看下: http://www.zhihu.com/question/20167122 http://www.cnblogs.com/DDark/archive/2011/11/28/2266 ...

  8. [Git] Git 的origin和master分析

    转载: http://lishicongli.blog.163.com/blog/static/1468259020132125247302/ 首先要明确一点,对git的操作是围绕3个大的步骤来展开的 ...

  9. selenium以手机模拟器方式打开Google浏览器

    使用chrome driver和chrome浏览器并进入chrome的 toggle device mode 模式,就可以很好的模拟手机端,下面直接上代码 public class runtest { ...

  10. [Angular] Tree shakable provider

    When we create a Service, Angluar CLI will helps us to add: @#Injectable({ providedIn: 'root' }) It ...