unity, write/read txt file
在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的更多相关文章
- Java read txt file
package com.Yang; import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;i ...
- Split CSV/TXT file
void Main(){ var path = @"c:\sourceGit\speciesLatLon.txt"; var inputLines = File.ReadAllLi ...
- Save matrix to a txt file - matlab 在matlab中将矩阵变量保存为txt格式
Source: Baidu Wenku % Original code has been modified dirMain = 'D:\test\'; fid = fopen([dirMain, 't ...
- VS Extension: Create a txt file and set the content
使用 Visual Studio Extension 创建一个文本文件,并填入内容. 需要引用 EnvDTE C:\Program Files (x86)\Microsoft Visual Studi ...
- [JS] save txt file
(function () { var blob = new Blob(['content'], {type: 'text/plain; charset=utf-8'}), blobUrl = URL. ...
- Big Txt File(一)
对于当今的数据集来说,动不动就上G的大小,市面的软件大多不支持,所以需要自己写一个. 常见的txt文本行形式存储的时候也不过是行数多些而已,可以考虑只观测部分行的方式,基于这个思路可以搞一个大数据的浏 ...
- python read txt file
refer to: http://www.jianshu.com/p/d8168034917c http://www.liaoxuefeng.com/wiki/001374738125095c955c ...
- 【软连接已存在,如何覆盖】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
- Java基础面试操作题: File IO 文件过滤器FileFilter 练习 把一个文件夹下的.java文件复制到另一个文件夹下的.txt文件
package com.swift; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File ...
随机推荐
- Linux命令之crontab
crontab [-u user] file crontab [-u user] [-l | -r | -e] [-i] [-s] crontab命令被用来提交和管理用户需要周期性执行的任务,与win ...
- Column 1 of table 'xxx' cannot be converted from type 'varchar(33)' to type 'varchar(11)'
mysql主从同步失败.错误日志如下. Column 1 of table 'xxx' cannot be converted from type 'varchar(33)' to type 'var ...
- 使用IIFE(立即执行函数)让变量私有化
今天去看了一个GITHUB上的开源项目,在客户端JS的脚本编写的时候,代码中多次使用了IIFE. 一开始我是懵逼的,不知道这种函数的意义何在,小菜鸟嘛. 后面我去研究了一番.发现了它的主要作用就是:让 ...
- 表(Table)
虽然我们已经将不同用途的物品保存在不同的仓库中了,但是在同一个仓库中数据的保存仍然存在问题.比如食品分为熟食.生肉.大米等,如果把他们随意的堆放在一起,就会造成我们无法很容易的对这些食品进行管理,当要 ...
- 【计算几何】【状压dp】Codeforces Round #226 (Div. 2) D. Bear and Floodlight
读懂题意发现是傻逼状压. 只要会向量旋转,以及直线求交点坐标就行了.(验证了我这俩板子都没毛病) 细节蛮多. #include<cstdio> #include<algorithm& ...
- lua 脚本之高级篇 (面向对象的完全支持),有性能问题。
---------------------------------------------------------- --面向对象核心库 ------------------------------- ...
- Xcode升级后导致插件不能用, 一句代码更新UUID OK~
find ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins -name Info.plist -maxdepth 3 | ...
- &#x开头的是什么编码?
在 Node 层利用 cheerio 解析网页时,输出的中文内容都是以 &#x 开头的一堆像乱码一样的东西,尝试过各种编码都无效,而且神奇的是,将这一堆“乱码”保存成网页后,通过浏览器打开又可 ...
- 【java】LocalDate和Date等新旧日期类的转化
// 01. java.util.Date --> java.time.LocalDateTime public void UDateToLocalDateTime() { java.util. ...
- PHP addslashes() 函数
定义和用法 addslashes() 函数在指定的预定义字符前添加反斜杠. 这些预定义字符是: 单引号 (') 双引号 (") 反斜杠 (\) NULL 语法 addslashes(stri ...