首页 > 其他 > 详细

c#如何读取txt文件内容

时间:2014-04-12 12:59:04      阅读:453      评论:0      收藏:0      [点我收藏+]

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.IO;
namespace test
{
    public partial class Text : System.Web.UI.Page
    {
      
        //读取txt文件的内容
        public string Gettext(string strfile)
        {
            string strout;
            strout = "";
            if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(strfile)))
            {

Console.Write("没有找到文件!");
            }
            else
            {
                StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath(strfile), System.Text.Encoding.Default);
                String input = sr.ReadToEnd();
                sr.Close();
                strout = input;
            }
            return strout;
        }

    }
}


讲解一下StreamReader:

实现一个 TextReader,使其以一种特定的编码从字节流中读取字符。


StreamReader(String, Encoding) 用指定的字符编码,为指定的文件名初始化 StreamReader 类的一个新实例。

System.Text.Encoding.Default
类型:System.Text.Encoding
操作系统的当前 ANSI 代码页的编码。

ReadToEnd 从流的当前位置到末尾读取所有字符。 (重写 TextReader.ReadToEnd()。)

StreamReader(String, Encoding) 用指定的字符编码,为指定的文件名初始化 StreamReader 类的一个新实例。

c#如何读取txt文件内容,布布扣,bubuko.com

c#如何读取txt文件内容

原文:http://blog.csdn.net/ztzi321/article/details/23473605

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!