在C#中,可以使用StreamReader
類來讀取文件中的數(shù)據(jù)。以下是一個簡單的示例代碼,演示如何從文件中讀取數(shù)據(jù):
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = "data.txt"; // 文件路徑
// 使用StreamReader讀取文件
using (StreamReader sr = new StreamReader(filePath))
{
string line;
while ((line = sr.ReadLine()) != null) // 逐行讀取文件內(nèi)容
{
Console.WriteLine(line);
}
}
}
}
在上述代碼中,首先創(chuàng)建了一個StreamReader
對象,并將文件路徑作為參數(shù)傳遞給它。然后,使用ReadLine()
方法逐行讀取文件內(nèi)容,并將每行數(shù)據(jù)打印到控制臺上。
請確保文件路徑正確,并確保有讀取文件的權(quán)限。