- 最后登录
- 2019-12-2
- 注册时间
- 2012-8-25
- 阅读权限
- 90
- 积分
- 34660
- 纳金币
- 38268
- 精华
- 111
|
unity3d中的信息保存到外部文件中。
C#代码如下:
using System.Collections;
using System.IO;
public class FileIO
{
public static bool WriteStringToFile(string filePath, string data, bool append)
{
try
{
StreamWriter sw = new StreamWriter(filePath, append);
sw.Write(data);
sw.Close();
return true;
}
catch (System.Exception err)
{
return false;
}
}
// Reads each line of a text file to a separate string which is stored
// in an ArrayList and returned.
public static ArrayList ReadFileToStrings(string filePath)
{
ArrayList list = new ArrayList();
string line;
// Read a file
try
{
StreamReader sr = new StreamReader(filePath);
line = sr.ReadLine();
if (line != null)
list.Add(string.Copy(line));
while (line != null)
{
line = sr.ReadLine();
if (line != null)
list.Add(string.Copy(line));
}
sr.Close();
return list;
}
catch (System.Exception err)
{
return list;
}
}
}
js代码:
import System.IO;
static function WriteStringToFile (filePath : String, data : String, append : boolean) : boolean {
try {
var sw = new StreamWriter(filePath, append);
sw.Write(data);
sw.Close();
return true;
}
catch (err) {
return false;
}
}
// Reads each line of a text file to a separate string which is stored
// in an array and returned.
function ReadFileToStrings (filePath : String) : Array {
var list = new Array();
// Read a file
try {
var sr = new StreamReader(filePath);
var line = sr.ReadLine();
while (line != null) {
list.Add(line);
line = sr.ReadLine();
}
sr.Close();
return list;
}
catch (err) {
return list;
}
}
|
|