0

嗨,我目前正在完成编程课上的最后一项作业,而且我对编程领域还很陌生。我的任务是通过 Streamreader 流式传输文档并提取信息以供菜单程序访问文档。

到目前为止,我只能流式传输

using System;
using System.IO;

class Test
{
    public static void Main()
    {
        try
        {
            Directory.SetCurrentDirectory(@"\Users\changl\Desktop");

            using (StreamReader sr = new StreamReader("earthquakes.csv"))
            {
                String line;

                while ((line = sr.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                }
            }
        }
        catch (Exception e)
        {

            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
        }
    }
}

我在编程方面相当有经验,并且希望对下一步将信息保存在文档中以供以后使用有所帮助。

4

2 回答 2

1

您只需用,, ;,分割每一行\t

var fieldsEnumerable = sr.ReadLine().Split(',');

但是这个库会为你做:

List<List<string>> records = new List<List<string>>();

using (CsvReader reader = new CsvReader(FilePath, Encoding.Default))
{
    while (reader.ReadNextRecord())
        records.Add(reader.Fields);
} 
于 2012-12-03T08:48:16.017 回答
1

Console.Writeline 只是将信息写入控制台窗口,您需要某种形式的方法来保存它..

using (StreamReader sr = new StreamReader("earthquakes.csv"))
    {
        String line;
        List<string> myList = new List<string>();

        while ((line = sr.ReadLine()) != null)
        {
           // Console.WriteLine(line);
            myList.add(line);
        }
    }
于 2012-12-03T08:41:55.743 回答