public string GetTxt(string fileName)
{
string line = string.Empty;
string strSql = "";
//string[] stringArray;
try
{
using (StreamReader sr = new StreamReader(fileName, Encoding.Default))
{ while ((line = sr.ReadLine()) != null)
{
string[] stringArray = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < stringArray.Length; i++)
{
if (stringArray[0].Length != 16)
{
continue;
}
strSql = i + "的值是:" + stringArray[i] + "<br />";
}
}
}
}
catch (Exception ex)
{
Response.Write("文件读取错误");
Response.Write(ex.Message);
} return strSql;
}protected void Page_Load(object sender, EventArgs e)
{
string fileName = @"D:\BegASPNET\WebSite1\txt\123.dat";
Response.Write(GetTxt(fileName));
}为什么只返回一条数据?
{
string line = string.Empty;
string strSql = "";
//string[] stringArray;
try
{
using (StreamReader sr = new StreamReader(fileName, Encoding.Default))
{ while ((line = sr.ReadLine()) != null)
{
string[] stringArray = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < stringArray.Length; i++)
{
if (stringArray[0].Length != 16)
{
continue;
}
strSql = i + "的值是:" + stringArray[i] + "<br />";
}
}
}
}
catch (Exception ex)
{
Response.Write("文件读取错误");
Response.Write(ex.Message);
} return strSql;
}protected void Page_Load(object sender, EventArgs e)
{
string fileName = @"D:\BegASPNET\WebSite1\txt\123.dat";
Response.Write(GetTxt(fileName));
}为什么只返回一条数据?
看看文件里面内容和 string[] stringArray = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries,这语句分割符对吗? strSql = i + "的值是:" + stringArray[i] + "<br />";
这条语句决定了返回的strSql 里面肯定是最后一行
{
if (stringArray[0].Length != 16)
{
continue;
}
strSql = i + "的值是:" + stringArray[i] + "<br />";
}
虽然这是个循环,但每当执行strSql = i + "的值是:" + stringArray[i] + "<br />";
这句话时候,他就覆盖了上一条记录,应该strSql = i + "的值是:" + stringArray[i] + "<br />";
改成strSql += i + "的值是:" + stringArray[i] + "<br />";
也就是把=改成+=就行了
public string GetTxt(string fileName)
{
string line = string.Empty;
string strSql = "";
//string[] stringArray;
try
{
using (StreamReader sr = new StreamReader(fileName, Encoding.Default))
{ while ((line = sr.ReadLine()) != null)
{
string[] stringArray = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < stringArray.Length; i++)
{
if (stringArray[0].Length != 16)
{
continue;
}
strSql += i + "的值是:" + stringArray[i] + "<br />";
}
}
}
}
catch (Exception ex)
{
Response.Write("文件读取错误");
Response.Write(ex.Message);
} return strSql;
}protected void Page_Load(object sender, EventArgs e)
{
string fileName = @"D:\BegASPNET\WebSite1\txt\123.dat";
Response.Write(GetTxt(fileName));
}
using System.Text;
...
public string GetTxt(string fileName)
{
string line = string.Empty;
//string strSql = "";
StringBuilder sb = new StringBuilder();
try
{
using (StreamReader sr = new StreamReader(fileName, Encoding.Default))
{ while ((line = sr.ReadLine()) != null)
{
string[] stringArray = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < stringArray.Length; i++)
{
if (stringArray[0].Length != 16)
{
continue;
}
sb.AppendLine( i + "的值是:" + stringArray[i]);
}
}
}
}
catch (Exception ex)
{
Response.Write("文件读取错误");
Response.Write(ex.Message);
} return sb.ToString();
} protected void Page_Load(object sender, EventArgs e)
{
string fileName = @"D:\BegASPNET\WebSite1\txt\123.dat";
Response.Write(GetTxt(fileName));
}