您的位置:首页 > 编程语言 > C#

C#读取txt数据存储到数组

2013-04-20 09:42 274 查看
public static void TextRead(out int[] data, string path)

{

data = new int[1];

try

{

FileStream fs = new FileStream(path, FileMode.Open);

using (StreamReader sr = new StreamReader(fs))

{

string line = sr.ReadToEnd().Replace("\r\n", " ");

string[] str2 = line.Split(' ');

int n = 0;

for (int i = 0; i < str2.Length; i++)

{

if (str2[i] != " ")

{

n++;

}

}

data = new int
;

n = 0;

for (int i = 0; i < str2.Length; i++)

{

if (str2[i] != " ")

{

data[n++] = Convert.ToInt32(str2[i]);

}

}

fs.Flush();

fs.Close();

}

}

catch (System.Exception e)

{

Console.Write("Error!{0}", e.Message);

Console.ReadKey();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: