109 lines
3.4 KiB
C#
109 lines
3.4 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using System.IO;
|
||
using System.Text;
|
||
|
||
public class TextReadAndWrite : MonoBehaviour
|
||
{
|
||
// Start is called before the first frame update
|
||
|
||
void Start()
|
||
{
|
||
|
||
}
|
||
|
||
|
||
//在streamingAssets文件夹下按名字读取,直接读取所有内容
|
||
private void LoadTxt(string Name) {
|
||
string UnityPath = Path.Combine(Application.streamingAssetsPath, Name+".txt");
|
||
StreamReader sr = new StreamReader(UnityPath, Encoding.GetEncoding("gb2312"));
|
||
string allText = sr.ReadToEnd();
|
||
Debug.Log(allText);
|
||
sr.Close();
|
||
}
|
||
|
||
|
||
//按文档位置读取文档里的信息并按照逐行输出成数组的形式读取
|
||
private void LoadTxt1(string position)
|
||
{
|
||
string UnityPath1 = Application.dataPath + position;
|
||
string[] allLineText = File.ReadAllLines(UnityPath1);
|
||
for (int i = 0; i < allLineText.Length; i++)
|
||
{
|
||
Debug.Log(allLineText[i]);
|
||
}
|
||
}
|
||
|
||
//纯路径写入文本 Hello Word
|
||
private void WriteTxt1()
|
||
{
|
||
string UnityPath2 = "C:\\Users\\zsjl\\Desktop\\MyTxt.txt";
|
||
File.AppendAllText(UnityPath2, "Hello Word", Encoding.UTF8);
|
||
}
|
||
|
||
//纯路径自动创建新文档并且写入数据,这是新文本
|
||
private void Write() {
|
||
string UnityPath3 = Application.dataPath + "/StreamingAssets/MyTxt.txt";
|
||
//"C:\\Users\\Administrator\\Desktop\\MyTxt.txt";
|
||
FileStream fs = new FileStream(UnityPath3, FileMode.Create);
|
||
StreamWriter writer = new StreamWriter(fs, Encoding.Default);
|
||
string str = "这是新文本";
|
||
writer.WriteLine(str);
|
||
writer.Close();
|
||
fs.Close();
|
||
}
|
||
|
||
//读取unity路径下的text文件中的第几行数据,并对数据按某种符号划分并存储数组
|
||
private void ReadTxtByRow(int row) {
|
||
string UnityPath = Application.dataPath + "/StreamingAssets/MyTxt.txt";
|
||
//将文档中所有行数据存入数组中
|
||
string[] rowText = File.ReadAllLines(UnityPath);
|
||
//获取文档中某一行数据并且按空格分割,存入新数组
|
||
string[] s = rowText[row].Split(new string[] { " " }, System.StringSplitOptions.RemoveEmptyEntries);
|
||
for (int j = 0; j < s.Length; j++)
|
||
{
|
||
Debug.Log(s[j]);
|
||
}
|
||
}
|
||
|
||
void OpenAndReadDate()
|
||
{
|
||
//文件夹为与Assets并列对的文件夹,同在一个项目文件夹下。
|
||
string FileName = "C:\\Users\\zsjl\\Desktop\\MyTxt.txt";
|
||
|
||
|
||
string[] strs = { "123", "321", "234" };
|
||
|
||
File.WriteAllLines(FileName, strs);
|
||
|
||
}
|
||
private void LoadTxt2()
|
||
{
|
||
string UnityPath = "C:\\Users\\zsjl\\Desktop\\MyTxt.txt";
|
||
StreamReader sr = new StreamReader(UnityPath, Encoding.GetEncoding("gb2312"));
|
||
string allText = sr.ReadToEnd();
|
||
Debug.Log(allText);
|
||
sr.Close();
|
||
}
|
||
|
||
private void Update()
|
||
{
|
||
OpenAndReadDate();
|
||
LoadTxt2();
|
||
// delete("C:\\Users\\zsjl\\Desktop\\MyTxt.txt");
|
||
}
|
||
//删除文档内容
|
||
public void DeleteTxt() {
|
||
string path = Application.dataPath + "/StreamingAssets/ShoppCart.txt";
|
||
File.WriteAllText(path, string.Empty);
|
||
print("成功");
|
||
}
|
||
//删除txt文档
|
||
public void delete(string position) {
|
||
File.Delete(@position);
|
||
}
|
||
|
||
|
||
}
|