如何使用流阅读器和文本阅读器向数组添加元素 [英] how to add elements to an array using stream reader..and text reader
本文介绍了如何使用流阅读器和文本阅读器向数组添加元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的桌面上有一个名为FINAL.TXT的文本文件...其中的值是
123456
i想复制使用流阅读器将这些值转换为数组...
i写了一段代码......但它给了我错误的值...
i已经使用了按钮和列表框...
这里是代码..
I have a text file called FINAL.TXT on my desktop...values in it are
123456
i want to copy these values into an array using stream reader...
i have written a piece of code...but it giving me wrong values...
i have taken a for with button and listbox in it...
here is the code..
button_click event
{
using (FileStream ar1=File.OpenRead(FINAL.TXT))
{
using (TextReader ART1 =new StreamReader(ar1))
{
int i;
for (i = 0; i < 6; i++)
{
array1[i] = Convert.ToInt32(ART1.Read());
listBox1.Items.Add(Convert.ToInt32(array1[i]));
}
}
}
}
推荐答案
否则读起来像这样。在这里你可以循环读取一个字符,直到达到EOF。
Otherwise read like this. Here you can read a single character in loop till it reaches EOF.
using (StreamReader reader1 = File.OpenText(@"final.txt"))
{
do
{
char next = (char)reader1.Read();
Console.WriteLine(next);
}
while(!reader1.EndOfStream);
}
尝试不同的方法...
Try a different approach...
string theFileContentsAsString = File.ReadAllText("final.txt");
int[] theStringAsArray = theFileContentsAsString.ToCharArray().Select(c => Convert.ToInt32(c.ToString())).ToArray();
//bind theStringArray to listbox here
这篇关于如何使用流阅读器和文本阅读器向数组添加元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文