Unity创建文件、读取文件
DirectoryInfo是c#中用来文件io的类,不支持web平台。
using UnityEngine;
using System;
using System.Text;//必须引用
using System.IO;//读写必备
using System.Collections;
public class CreateFile : MonoBehaviour {
byte[] myByte=new byte[100];
char[] myChar=new char[100];
string s="";
// Use this for initialization
void Start () {
Create();
}
// Update is called once per frame
void Update () {
}
void Create()
{
string path=Application.dataPath + "//file.txt";
//创建文件
//File.Create(path);
DirectoryInfo myDirectoryInfo=new DirectoryInfo(path);
if(myDirectoryInfo.Exists)
print("this file already exists!");
else print("create file.txt!");
//创建文件夹
//Directory.CreateDirectory(Application.dataPath + "/Files");
//缓冲类BufferedStream读取文件
int c;
BufferedStream myBS=new BufferedStream(new FileStream(path ,FileMode.Open));
while((c=myBS.ReadByte())!=-1)
{
s+=((int)c).ToString();
print(((char)c).ToString());
}
print(s);
/*
FileStream myFS=new FileStream(path,FileMode.OpenOrCreate);
myChar="My first file!".ToCharArray();
Encoder myEncoder=Encoding.UTF8.GetEncoder();
myEncoder.GetBytes(myChar,0,myChar.Length,myByte,0,true);//设置流的位置从0开始
myFS.Write(myByte,0,myByte.Length); //将字节数组写入文件
*/
}
}
- 下一篇:使用余玄函数实现补间缓动
- 上一篇:异步加载(ngui版)