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;
}
}
引用位置:
http://forum.unity3d.com/viewtopic.php?t=15196&postdays=0&postorder=asc&start=0
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;
}
}
引用位置:
http://forum.unity3d.com/viewtopic.php?t=15196&postdays=0&postorder=asc&start=0
本文提供了一段使用C#和JavaScript进行文件读写操作的代码示例,包括如何将字符串写入文件及从文本文件中逐行读取数据,并将结果存储在ArrayList中。
7538

被折叠的 条评论
为什么被折叠?



