如何读取文本文件并将其存储为数组格式. [英] how read text file and store it into array format.

查看:96
本文介绍了如何读取文本文件并将其存储为数组格式.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好先生
我今天很舒服,

我正在开发应用程序c锐胜形式.而且我想阅读textFile.

并以字符串数组格式存储.
请给出解决方案.

如何读取文本文件并将其存储到字符串array.and我的文本文件包含约20000行.

Hi Sir
I am uday,

I am developing application c sharp win form. and I want to read textFile.

and store in string array format.
please give solution.

How to read text file and store it into string array.and my text file contains arount 20000 rows.

推荐答案


使用StreamReader一次读取一行文件,然后将每一行添加到List<string>中.然后在其上使用.ToArray().
Use a StreamReader to read the file a line at a time, then add each row to a List<string>. Then use .ToArray() on that.


首先创建一个按钮和文本框,然后尝试使用此代码.

private void button1_Click(对象发送者,EventArgs e)
{

字符串fpath ="E:\\ one.txt";
StreamReader dr =新的StreamReader(fpath);
string [] arr =新字符串[1000];
int i = 0;
字符串st =";
while(dr.Peek()!=-1)
{
arr [i] = dr.ReadLine();
st = st + arr [i] .ToString()+"\ n";
i ++;
}
textBox1.Text = st;
}

在这里,我声明了字符串数组,大小为1000,并从读取器获取值.

我希望这对您有用;
再见
first make one button and textbox and try this code.

private void button1_Click(object sender, EventArgs e)
{

string fpath="E:\\one.txt";
StreamReader dr = new StreamReader(fpath);
string[] arr=new string[1000];
int i = 0;
string st = "";
while (dr.Peek() !=-1)
{
arr[i] = dr.ReadLine();
st = st + arr[i].ToString()+"\n";
i++;
}
textBox1.Text = st;
}

here i declared string array and that size is 1000, and get value from reader.

i hope this is useful to you;
take care bye bye


这篇关于如何读取文本文件并将其存储为数组格式.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆