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 ...
随机推荐
- Codeforces Round 536 (Div. 2) (E)
layout: post title: Codeforces Round 536 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - L Doki Doki Literature Club
Doki Doki Literature Club Time Limit: 1 Second Memory Limit: 65536 KB Doki Doki Literature Club ...
- POJ 3168 Barn Expansion (几何基础)
[题目链接] http://poj.org/problem?id=3168 [题目大意] 给出一些矩形,没有相交和包含的情况,只有相切的情况 问有多少个矩形没有相切或者边角重叠 [题解] 我们将所有的 ...
- [CF460E]Roland and Rose
题意:给定$n$和$r$,要找$n$个整点,使得他们两两距离的平方和最大,并且所有点到原点的距离必须小于$r$ 很容易猜到答案在凸包上然后暴力找,但证明还是挺妙的 首先转化一下距离平方和 令$\vec ...
- 【最大权闭合子图/最小割】BZOJ3438-小M的作物【待填】
[题目大意] 小M在MC里开辟了两块巨大的耕地A和B(你可以认为容量是无穷),现在,小P有n中作物的种子,每种作物的种子有1个(就是可以种一棵作物)(用1...n编号),现在,第i种作物种植在A中种植 ...
- C# 二维码扫描
Zint类用于产生二维码.https://sourceforge.net/projects/zint/ Zxing类用于读取二维码. https://github.com/zxing/zxing AF ...
- python语言实现阶乘的两种方法---递归和迭代
阶乘的递归实现,代码如下: def factorial(n): if n==1: return 1 else: return n*factorial(n-1) number = int(input(& ...
- C#正则表达式开源工具
先交代一下背景,最近工作中经常用到正则表达式,而正则表达式这个东西我个人觉得很鸡肋,不用吧,有些功能实现起来会很麻烦.用吧,又不是说工作中经常用到,只是有时候有些需要求用到而已.但是正则表达式只要一段 ...
- Nginx 编译参数详解/大全
Nginx参数: –prefix= 指向安装目录 –sbin-path 指向(执行)程序文件(nginx) –conf-path= 指向配置文件(nginx.conf) –error-log-path ...
- Centos7.3 bbc tools安装
http://blog.csdn.net/orangleliu/article/details/54099528 更新到最新 CentOS 7.3 1611 yum update -y cat /et ...