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>
。
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<string>
中。
三、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>
。
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<string>
中。
七、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<string>
中。