如何从文本文件中读取并存储到 C# 中的数组列表?
How to read from a text file and store to array list in c#?
我正在尝试读取文本文件并将其数据存储到数组列表中。它的工作没有任何错误。我的文本文件里面是这样的。
277.18
311.13
349.23
277.18
311.13
349.23
277.18
311.13
349.23
但在控制台输出中我可以看到这么多数据。
277.18
311.13
349.23
349.23
**329.63
329.63
293.66
293.66
261.63
293.66
329.63
349.23
392**
277.18
311.13
349.23
277.18
311.13
349.23
277.18
311.13
349.23
我的文本文件中也没有粗体数字。
这是我的代码。如何解决这个问题??有人可以帮帮我吗..拜托...
OpenFileDialog txtopen = new OpenFileDialog();
if (txtopen.ShowDialog() == DialogResult.OK)
{
string FileName = txtopen.FileName;
string line;
System.IO.StreamReader file = new System.IO.StreamReader(FileName.ToString());
while ((line = file.ReadLine()) != null)
{
list.Add(double.Parse(line));
}
//To print the arraylist
foreach (double s in list)
{
Console.WriteLine(s);
}
}
我认为你的list
已经包含了一些数据,你应该在添加新文件数据之前清除它。
OpenFileDialog txtopen = new OpenFileDialog();
if (txtopen.ShowDialog() == DialogResult.OK)
{
list.Clear(); // <-- clear here
string FileName = txtopen.FileName;
string line;
System.IO.StreamReader file = new System.IO.StreamReader(FileName.ToString());
while ((line = file.ReadLine()) != null)
{
list.Add(double.Parse(line));
}
//To print the arraylist
foreach (double s in list)
{
Console.WriteLine(s);
}
}
除此之外,这里似乎一切都很好。
尝试使用 Linq,它不会向 现有列表 添加任何内容:
if (txtopen.ShowDialog() == DialogResult.OK) {
var result = File
.ReadLines(txtopen.FileName)
.Select(item => Double.Parse(item, CultureInfo.InvariantCulture));
// if you need List<Double> from read values:
// List<Double> data = result.ToList();
// To append existing list:
// list.AddRange(result);
Console.Write(String.Join(Environment.NewLine, result));
}
我正在尝试读取文本文件并将其数据存储到数组列表中。它的工作没有任何错误。我的文本文件里面是这样的。
277.18
311.13
349.23
277.18
311.13
349.23
277.18
311.13
349.23
但在控制台输出中我可以看到这么多数据。
277.18
311.13
349.23
349.23
**329.63
329.63
293.66
293.66
261.63
293.66
329.63
349.23
392**
277.18
311.13
349.23
277.18
311.13
349.23
277.18
311.13
349.23
我的文本文件中也没有粗体数字。 这是我的代码。如何解决这个问题??有人可以帮帮我吗..拜托...
OpenFileDialog txtopen = new OpenFileDialog();
if (txtopen.ShowDialog() == DialogResult.OK)
{
string FileName = txtopen.FileName;
string line;
System.IO.StreamReader file = new System.IO.StreamReader(FileName.ToString());
while ((line = file.ReadLine()) != null)
{
list.Add(double.Parse(line));
}
//To print the arraylist
foreach (double s in list)
{
Console.WriteLine(s);
}
}
我认为你的list
已经包含了一些数据,你应该在添加新文件数据之前清除它。
OpenFileDialog txtopen = new OpenFileDialog();
if (txtopen.ShowDialog() == DialogResult.OK)
{
list.Clear(); // <-- clear here
string FileName = txtopen.FileName;
string line;
System.IO.StreamReader file = new System.IO.StreamReader(FileName.ToString());
while ((line = file.ReadLine()) != null)
{
list.Add(double.Parse(line));
}
//To print the arraylist
foreach (double s in list)
{
Console.WriteLine(s);
}
}
除此之外,这里似乎一切都很好。
尝试使用 Linq,它不会向 现有列表 添加任何内容:
if (txtopen.ShowDialog() == DialogResult.OK) {
var result = File
.ReadLines(txtopen.FileName)
.Select(item => Double.Parse(item, CultureInfo.InvariantCulture));
// if you need List<Double> from read values:
// List<Double> data = result.ToList();
// To append existing list:
// list.AddRange(result);
Console.Write(String.Join(Environment.NewLine, result));
}