110,538
社区成员
发帖
与我相关
我的任务
分享
class Program
{
static void Main(string[] args)
{
FileStream fs = null;
StreamReader r = null;
string str;
int row;
try
{
fs = new FileStream("data.txt", FileMode.Open);
r = new StreamReader(fs);
do
{
Console.WriteLine("请输入要查询的行数(>0):");
str = Console.ReadLine();
if (!(int.TryParse(str, out row)))
Console.WriteLine("请输入整数!");
} while (row < 1);
for (int i = 0; i < row - 1 && !r.EndOfStream; r.ReadLine(), i++) ;
str = r.ReadLine();
Console.WriteLine("查到的行:");
Console.WriteLine(str.Substring(str.IndexOf('"') + 1, str.LastIndexOf('"') - str.IndexOf('"') - 1));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (r != null)
r.Close();
if (fs != null)
fs.Close();
}
}
}