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 ...
随机推荐
- UVA 839 Not so Mobile (递归建立二叉树)
题目连接:http://acm.hust.edu.cn/vjudge/problem/19486 给你一个杠杆两端的物体的质量和力臂,如果质量为零,则下面是一个杠杆,判断是否所有杠杆平衡. 分析:递归 ...
- 【kmp算法】poj2185 Milking Grid
先对每行求出所有可能的循环节长度(不需要整除). 然后取在所有行中都出现了的,且最小的长度为宽. 然后将每一行看作字符,对所有行求next数组,将n-next[n](对这些行来说最小的循环节长度)作为 ...
- 【块状树】【树链剖分】bzoj1036 [ZJOI2008]树的统计Count
很早之前用树链剖分写过,但是代码太长太难写,省选现场就写错了. #include<cstdio> #include<algorithm> #include<cstring ...
- 【树上莫队】【带修莫队】bzoj3052 [wc2013]糖果公园
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using ...
- 【MySQL笔记】数据操纵语言DML
1.数据插入 INSERT INTO table_name (列1, 列2,...) VALUES(值1, 值2,....),(第二条),(第三条)... 注: 1)如果表中的每一列均有数据插 ...
- nginx 隐藏index.php 并开启rewrite日志调试(apache也有)
开启rewrite 日志 error_log /data/log/nginx/error.log notice; 位于最外层,大约在文件的前几行 再在http{}括号里增加一行:rewri ...
- GNS3 思科模块 说明
GNS3整合了如下的软件: Dynamips :一款可以让用户直接运行Cisco系统(IOS)的模拟器 Dynagen :是Dynamips的文字显示前端 Pemu : PIX防火墙设备模 ...
- [Android Traffic] Android网络开启、关闭整理
转载: http://blog.csdn.net/tu_bingbing/article/details/8469871 近段时间由于要对手机网络状况进行判断.开启和关闭,从网上找了些资料,现整理如下 ...
- golangWEB框架gin学习之路由群组
原文地址:http://www.niu12.com/article/42 package main import ( "github.com/gin-gonic/gin" &quo ...
- SQLSERVER调用DLL程序
在SQL Server中调用dll分为两个步骤 1.创建一个dll文件(dll文件分成3种类型,讲其中一种) 2.把dll文件放进SQL Server的程序集中.然后定义一个Function,就可以通 ...