C#读取txt文本文件(dat)的方法

 

  
  
  
  
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Web; 
  5. using System.Web.UI; 
  6. using System.Web.UI.WebControls; 
  7. using System.IO; 
  8. using System.Text; 
  9. using System.Collections; 
  10.  
  11. public partial class _Default : System.Web.UI.Page 
  12.     protected void Page_Load(object sender, EventArgs e) 
  13.     { 
  14.         string filename = @"E:\datread\kg.dat"
  15.         string line; 
  16.         string[] stringArray; 
  17.         try 
  18.         { 
  19.             using (StreamReader sr = new StreamReader(filename, Encoding.Default)) 
  20.             { 
  21.                 while ((line = sr.ReadLine()) != null
  22.                 { 
  23.                     stringArray = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); 
  24.                     for (int i = 0; i < stringArray.Length; i++) 
  25.                     { 
  26.                             Response.Write(i + "的值是:" + stringArray[i]+"<br />"); 
  27.                     } 
  28.                 } 
  29.             } 
  30.         } 
  31.         catch (Exception ex) 
  32.         { 
  33.             Response.Write("The file could not be read:"); 
  34.             Response.Write(ex.Message); 
  35.         } 
  36.  
  37.     } 

 

你可能感兴趣的:(职场,文本文件,休闲)