求Response.Write无法输出的原因(拜)
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
StreamWriter sw = File.CreateText(Server.MapPath ( ". ")+ "//hello.txt ");
sw.WriteLine( "你好: ");
sw.WriteLine( "欢迎学习Asp.Net ");
sw.Flush();
sw.Close();
StreamReader sr = File.OpenText(Server.MapPath( ". ") + "//hello.txt ");
//StringBuilder sb = new StringBuilder();//提高效率的方法
string s1;
while ((s1 = sr.ReadLine()) != null)
{
//Response.Write(s1 + " <br> ");//在这里可以正常输出两行文本
//sb.Append(s1+ " </br> ");//提高效率的方法
s1 += s1 + " <br> ";//
}
// Response.Write(sb);
// Response.Write (s1);//放在这里居然什么也没输出,怎么回事???s1的值跑哪里去了?被谁吃了?!
sr.Close();
}
}
------解决方案--------------------建议在while结束前跟踪s1, 还有s1 += s1 + " <br> "; 好象是s1 += " <br> ";吧。
------解决方案--------------------while ((s1 = sr.ReadLine()) != null)
退出循环的时候当然 null 了
------解决方案--------------------当结束的时候 s1 = sr.ReadLine() s1被赋值为null
string s1;
string s2 = string.Empty;
while ((s1 = sr.ReadLine()) != null)
{
s2 += s1 + " <br> ";
}
Response.Write (s2);