readnews.cs

来自「asp.net_Lesson8.ASP.NET经典范例50讲.第8讲登场」· CS 代码 · 共 63 行

CS
63
字号
using System;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;

public class News
{
  string subject;
  string author;
  string contentFile;

  public News(string subject, 
              string author, 
              string contentFile)
  {
    this.subject = subject;
    this.author = author;
    this.contentFile = contentFile;
  }
  public string Subject
  {
    get { return subject; }
  }

  public string Author
  {
    get { return author; }
  }

  public string ContentFile
  {
    get { return contentFile; }
  }

  public static ArrayList ReadNewsFromFile(string fileName)
  {
    StreamReader reader = null;
    ArrayList list = new ArrayList();
    try
    {
      reader = File.OpenText(fileName);					
      string line;
      string[] words;
      while( (line = reader.ReadLine()) != null)
      {
        words = Regex.Split(line, ",");
        News news = new News(words[0], words[1], words[2]);
        list.Add(news);
      }
      return list;
    }
    catch(FileNotFoundException e)
    {
      return list;
    }
    finally
    {
      if(reader != null)
        reader.Close();
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?