在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. 转 select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET

    select函数用于在非阻塞中,当一个套接字或一组套接字有信号时通知你,系统提供select函数来实现多路复用输入/输出模型,原型:int select(int maxfd,fd_set *rdset ...

  2. [Atcoder Regular Contest 062] Tutorial

    Link: ARC 062 传送门 C: 每次判断增加a/b哪个合法即可 并不用判断两个都合法时哪个更优,因为此时两者答案必定相同 #include <bits/stdc++.h> usi ...

  3. 【CCpp程序设计2017】迷宫游戏

    大一寒假作业!写了第一个小游戏! //maze_test By lizitong #include<stdio.h> #include<time.h> #include< ...

  4. C语言计算器

    地址:  https://wenda.so.com/q/1371173683061754?src=140

  5. 生成唯一code

    private String getCode() { List<String> ptypeCodeList = mapper.findCodeList(); String code = & ...

  6. ListView控件(上)数据适配器:ListView绑定监听是SetOnItemClickListener

    (一) 1.效果图: 2.MainActivity.java package com.example.app5; import android.support.v7.app.AppCompatActi ...

  7. less 命令详解!

    less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性.在 more 的时候,我们并没有办法向前面翻 ...

  8. 开源用户界面和布局的套件XiaoCai.WinformUI(美化用户界面利器)

    发布一款开源用户界面和布局的套件,请朋友们多提提宝贵建议! XiaoCai.WinformUI主要是解决用户界面和布局的套件,能够快速进行合理性布局,美化用户界面. 因为之前发布到谷歌里,好多朋友都说 ...

  9. Python+C混编

    Python最慢!C最快!Python+C混编?结果可想而知! 樱桃种子 百家号04-1712:11 共享库 使用C语言编译产生共享库,然后python使用ctype库里的cdll来打开共享库. 举例 ...

  10. Accessing Report Server using Report Server Web Service - Microsoft SQL Server 2008R2

    Today I am going to write about how to access SQL Server Report Server through Report Server Web ser ...