天天看点

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string filename = @"E:\datread\kg.dat";
        string line;
        string[] stringArray;
        try
        {
            using (StreamReader sr = new StreamReader(filename, Encoding.Default))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    stringArray = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < stringArray.Length; i++)
                    {
                            Response.Write(i + "的值是:" + stringArray[i]+"<br />");
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write("The file could not be read:");
            Response.Write(ex.Message);
        }

    }
}