using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System;
public class DoFileTest : MonoBehaviour {
public Text PathText;
public Text ResultText;
public InputField TestField;
public string filepath;
// Use this for initialization
void Start ()
{
filepath = Application.persistentDataPath + "/file.txt";
PathText.text = filepath;
}
public void ReadFile()
{
try
{
StreamReader sr = new StreamReader(filepath);
string ss = sr.ReadToEnd();
Debug.Log(ss);
ResultText.text = ss;
}catch(Exception e)
{
ResultText.text = e.Message;
}
}
public void OnBtnClick()
{
Debug.Log(TestField.text + " shengc");
try
{
FileStream fs = new FileStream(filepath, FileMode.Create);
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(TestField.text);
fs.Write(bytes, 0, bytes.Length);
fs.Flush();
fs.Close();
}
catch(Exception e)
{
Debug.Log("保存失败!!!!");
}
}
}
unity3d读取外部文件详细 :
网友评论