C#讀取文件全面解析

C#作為一門高級編程語言,有著強大的文件操作功能。本篇文章將從多個方面對C#讀取文件進行詳細的闡述,分別包括:cba賽程、coach、CCTV5節目表、CBA、CCTV5在線直播、C1駕照、C反應蛋白高、chat GPT人工智慧和C羅。

一、cba賽程

C#讀取文件可以輕鬆實現cba賽程的獲取。通過StreamReader類,我們可以方便地按行讀取文本文件,並將其存儲在字元串中。

string filePath = "cba.txt";
using (StreamReader sr = new StreamReader(filePath))
{
    string line;
    while ((line = sr.ReadLine()) != null)
    {
        Console.WriteLine(line);
    }
}

上述代碼中,我們首先指定了文件路徑,然後使用StreamReader類通過using語句打開文件,並按行讀取文件內容,最後將每一行輸出到控制台上。

二、coach

C#讀取文件同樣適用於獲取coach的信息。下面的示例演示了如何按照逗號分隔符讀取CSV文件,並將其存儲為List。

string filePath = "coach.csv";
List<string> coachs = new List<string>();
using (StreamReader sr = new StreamReader(filePath))
{
    string line = sr.ReadLine(); // 第一行為標題,不需保存
    while ((line = sr.ReadLine()) != null)
    {
        string[] coach = line.Split(',');
        coachs.Add(coach[0] + " " + coach[1]); // 只保存姓名
    }
}

上述代碼將CSV文件逐行讀取,並使用分隔符將每一行拆分為多個元素,然後將姓名存儲到List中

三、CCTV5節目表

如何實現C#讀取文件獲取CCTV5的節目表呢?下面的代碼演示了如何按列讀取含有表格數據的HTML文件,並將其轉換為DataTable。

string filePath = "cctv5.html";
DataTable dt = new DataTable();
dt.Columns.Add("時間");
dt.Columns.Add("節目名稱");
using (StreamReader sr = new StreamReader(filePath))
{
    string line;
    while ((line = sr.ReadLine()) != null)
    {
        if (line.Contains("<td>"))
        {
            string[] cells = line.Split(new string[] { "<td>", "</td>" },
                StringSplitOptions.RemoveEmptyEntries);
            dt.Rows.Add(cells[1], cells[2]); // 只保存時間和節目名稱
        }
    }
}

上述代碼將HTML文件逐行讀取,並使用分隔符將每一行拆分為多個元素,然後將時間和節目名稱存儲到DataTable中。

四、CBA

如果我們需要將多個CSV文件中的數據進行合併,C#讀取文件同樣可以勝任。下面的代碼演示了如何將兩個CSV文件中的數據合併到一個DataTable中。

string filePath1 = "cba1.csv";
string filePath2 = "cba2.csv";
DataTable dt = new DataTable();
dt.Columns.Add("日期");
dt.Columns.Add("球隊");
dt.Columns.Add("比分");
using (StreamReader sr1 = new StreamReader(filePath1))
{
    string line;
    while ((line = sr1.ReadLine()) != null)
    {
        string[] cells = line.Split(',');
        dt.Rows.Add(cells[0], cells[1], cells[2]);
    }
}
using (StreamReader sr2 = new StreamReader(filePath2))
{
    string line;
    while ((line = sr2.ReadLine()) != null)
    {
        string[] cells = line.Split(',');
        dt.Rows.Add(cells[0], cells[1], cells[2]);
    }
}

上述代碼將兩個CSV文件逐行讀取,並將每一行數據拆分為多個元素,然後將所有數據存儲到同一個DataTable中。

五、CCTV5在線直播

如何從C#讀取文件中提取指定位置的數據?下面的代碼演示了如何使用BinaryReader類從二進位文件中提取指定位置、指定長度的數據。

string filePath = "cctv5.bin";
int pos = 15; // 起始位置
int len = 32; // 讀取長度
using (BinaryReader br = new BinaryReader(File.Open(filePath, FileMode.Open)))
{
    br.BaseStream.Seek(pos, SeekOrigin.Begin);
    byte[] result = br.ReadBytes(len);
    Console.WriteLine(Encoding.Default.GetString(result)); // 顯示讀取結果
}

上述代碼將指定位置和長度的二進位數據讀取出來,並使用Encoding.Default將其轉換為字元串。

六、C1駕照

C#讀取文件同樣適用於解析XML文檔。下面的代碼演示了如何讀取含有嵌套元素的XML文檔,並將其轉換為List。

string filePath = "c1.xml";
List<string> drivers = new List<string>();
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
XmlNodeList nodes = doc.SelectNodes("//item");
foreach (XmlNode node in nodes)
{
    string name = node.Attributes["key"].Value;
    string value = node.InnerText;
    drivers.Add(name + " " + value); // 只保存姓名和駕照類型
}

上述代碼將XML文件解析為XmlDocument,並使用XPath語法選擇符合條件的節點,最後將姓名和駕照類型存儲到List中。

七、C反應蛋白高

如果我們需要在C#讀取文件時忽略某些行,下面的代碼演示了如何使用Skip方法跳過一定數量的行。

string filePath = "crp.txt";
List<string> values = new List<string>();
using (StreamReader sr = new StreamReader(filePath))
{
    sr.ReadLine(); // 忽略第一行
    sr.ReadLine(); // 忽略第二行
    string line = sr.ReadLine(); // 從第三行開始讀取數據
    while (line != null)
    {
        string[] cells = line.Split('\t');
        values.Add(cells[1]); // 只保存第二列數據
        line = sr.ReadLine();
    }
}

上述代碼使用StreamReader類按行讀取文本文件,並使用Split方法將每行數據拆分為多個元素,最後使用Skip方法跳過前兩行。

八、chat GPT人工智慧

如果我們需要實現多種格式的文件讀取,C#同樣可以實現。下面的代碼演示了如何使用不同的文件讀取類來讀取不同格式的文件,並將其存儲到同一個List中。

string filePath1 = "cba.txt";
string filePath2 = "coach.csv";
string filePath3 = "cctv5.html";
List<string> contents = new List<string>();
using (StreamReader sr1 = new StreamReader(filePath1))
{
    contents.Add(sr1.ReadToEnd()); // 讀取txt文件
}
using (StreamReader sr2 = new StreamReader(filePath2))
{
    string line = sr2.ReadLine(); // 第一行為標題,不需保存
    while ((line = sr2.ReadLine()) != null)
    {
        string[] cells = line.Split(',');
        contents.Add(cells[0] + " " + cells[1]); // 只保存姓名
    }
}
using (StreamReader sr3 = new StreamReader(filePath3))
{
    string line;
    while ((line = sr3.ReadLine()) != null)
    {
        if (line.Contains("<p>"))
        {
            contents.Add(line); // 保存HTML文件中的段落
        }
    }
}

上述代碼將不同格式的文件分別使用不同的文件讀取類進行讀取,並將所有內容存儲到同一個List中。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/237397.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 12:04
下一篇 2024-12-12 12:05

相關推薦

發表回復

登錄後才能評論