【问题标题】:Print from text file to console using a selected method使用选定的方法从文本文件打印到控制台
【发布时间】:2017-05-24 03:49:05
【问题描述】:

所以我似乎无法在网上找到任何关于此的信息。 所以我想知道如何通过一种方法将文本文件中的数据打印到控制台应用程序。

这是一个例子, 这是Student类,我想用作布局类型的方法是DisplayInfo():

class Student
{
    public string ID { get; set; }
    public double Score { get; set; }

    public Student(string SID, double SC)
    {
        ID = SID;
        Score = SC;
    }

    public void DisplayInfo()
    {
        Console.WriteLine(ID,": " + Score);
    }
}

这是迄今为止我在 PrintData void 中的内容:

public void PrintData()
{
    using (StreamReader readtext = new StreamReader("data.txt"))
    {
        string readMeText = readtext.ReadLine();

    }
}

基本上,我想在 PrintData() void 内以 DisplayInfo() 中显示的格式打印它。

【问题讨论】:

  • Console.WriteLine(ID,": " + Score); 有错误,可能是Console.WriteLine(ID+": " + Score);

标签: c# visual-studio file text streamreader


【解决方案1】:

假设您有这种格式的文本 (demo.txt) 文件:

s1 140
s2 250
s3 400

其中s1,s2,s3 是学生ID,140,250,400 是他们的分数。

现在您需要从文件中读取行,而它不是 End of Stream 通过空格拆分读取行并写入控制台。以下函数正在做正确的事情:

public void PrintData()
{
    using (var readtext = new StreamReader(@"c:\Users\IIG\Desktop\demo.txt"))
    {
        while (!readtext.EndOfStream)
        {
            string currentLine = readtext.ReadLine();
            var args = currentLine.Split(' ');
            if (args.Length > 1)
            {
                Console.WriteLine(args[0] + ":" + args[1]);
            }
            else
            {
                Console.WriteLine("Invalid line");
            }
        }
    }
}

更新

以下是如何使用对象序列化/反序列化的示例:

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace ConsoleApplication4
{
    public class Program
    {
        static void Main(string[] args)
        {
            List<Student> students = new List<Student>();
            students.Add(new Student("s1", 140));
            students.Add(new Student("s2", 200));
            students.Add(new Student("s3", 250));
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Student>));
            if(!File.Exists(@"c:\Users\IIG\Desktop\demo.txt"))
            {
                using (File.Create(@"c:\Users\IIG\Desktop\demo.txt"))
                {

                }

            }
            using (var writer = new StreamWriter(@"c:\Users\IIG\Desktop\demo.txt"))
            {
                xmlSerializer.Serialize(writer,students);
            }
            PrintData();
        }
        [Serializable]
        public class Student
        {
            public string ID { get; set; }
            public double Score { get; set; }

            public Student()
            {

            }
            public Student(string SID, double SC)
            {
                ID = SID;
                Score = SC;
            }
            public void DisplayInfo()
            {
                Console.WriteLine(ID+ ": " + Score);
            }
        }
        public static void PrintData()
        {
            using (var readtext = new StreamReader(@"c:\Users\IIG\Desktop\demo.txt"))
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Student>));
                List<Student> readStudents = (List<Student>)xmlSerializer.Deserialize(readtext);
                foreach (var student in readStudents)
                {
                    student.DisplayInfo();
                }

            }
        }
    }
}

【讨论】:

  • @William.K1 是的,这是可能的,但是当您将学生保存在文件中时,您必须将学生对象序列化为文本文件,而不是在您想从文件中读取并写入控制台时反序列化
  • 不太清楚你的意思。每当我在 PrintData 中尝试使用 DisplayInfo 进行任何操作时,它只是说它在当前上下文中不存在。
  • @William.K1 我已经用示例更新了我的答案
  • 这太疯狂了。谢谢你:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多