c#讀mif檔案及txt檔案代碼

來源:互聯網
上載者:User

作者/收集者:wiserish

開發環境:C#

代碼:

c#讀mif檔案及txt檔案代碼
using System;
using System.IO;
using System.Collections;

namespace ReadMif
{
     class LoadMif
     {
         //attribute
         private string m_File_Name;
         private System.IO.StreamReader m_fReader;     
         private System.Collections.ArrayList m_Sentence;
         private string[][] m_Words;    
  
        //private methord
        private void OpenFile()
       {
             try
             {
if (m_File_Name==null)  
      throw new ArgumentNullException();
if (!File.Exists(m_File_Name))  
                     throw new ArgumentException("Invalid file name"+m_File_Name);
if (Path.GetExtension(m_File_Name)!=null&&!m_File_Name.EndsWith(".mif")&&!m_File_Name.EndsWith(".txt"))          throw new Exception("Invalid file type!");
        }
//捕獲異常
catch (ArgumentNullException ane)
{
      Console.WriteLine(ane.ToString());
      return;
}
catch (ArgumentException ae)
{
      Console.WriteLine(ae.ToString());
      return;
}
catch (Exception e)
{
                     Console.WriteLine(e.ToString());
      return;
}
m_fReader=File.OpenText(m_File_Name);//設定開啟並要進行讀取的檔案
     }

       private void ReadFile()
       {
string text_line;
m_Sentence=new ArrayList();   //初始化arraylist對象
while ((text_line=m_fReader.ReadLine())!=null);//只要檔案未結束
{
      if (text_line.Length==0)   //空行不讀
          continue;
      m_Sentence.Add(text_line); //將讀到的行添加到arraylist對象中
}

     }

     private void PrintContext()
     {
foreach(string line in m_Sentence)
{
      System.Console.WriteLine(line);
}
     }

     //public methord
     public void LoadMifFile(string mif_file)      //mif_file為該類與外界的介面,從外界傳入檔案名稱
   {
       m_File_Name=mif_file;
                 //用成員變數儲存外界資料mif_file,用類成員變數實現內部資料流動
//公用成員方法調用私人成員方法實現了底層操作的封裝
//使用者只需也只能調用LoadMifFile來實現讀檔案操作
       OpenFile();
       ReadFile();
       PrintContext();
   }   //end of loadmiffile function
}   //end of LoadMif class
}   //end of namespace

//通過外界主程式調用LoadMif類以實現讀mif檔案
class MainEntry
{
     static void Main(string[] args)
     {
         ReadMif.LoadMif mif=new ReadMif.LoadMif();     //定義LoadMif類的執行個體並初始化
         if (args.Length==0)
Console.WriteLine(@"Useage: ReadMif miffile.mif to print mif file context     on the screen.
For example:
ReadMif test.mif");
         else
             mif.LoadMifFile(args[0]);
         return;
   }   //end of main
}// end of MainEntry class

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.