前言
文本讀取在上位機(jī)開發(fā)中經(jīng)常會(huì)使用到,實(shí)現(xiàn)的方式也有很多種,今天跟大家分享一下C#實(shí)現(xiàn)讀取讀取的7種方式。
這里我們先寫好了一個(gè)測(cè)試界面,提供一個(gè)文件路徑選擇的入口,具體如下所示:
方式一
基于FileStream,并結(jié)合它的Read方法讀取指定的字節(jié)數(shù)組,最后轉(zhuǎn)換成字符串進(jìn)行顯示。
this.rtb_Content.Clear();
FileStream fs = new FileStream(this.txt_FilePath.Text, FileMode.Open, FileAccess.Read);
int n = (int)fs.Length;
byte[] b = new byte[n];
int r = fs.Read(b, 0, n);
fs.Close();
this.rtb_Content.Text = Encoding.UTF8.GetString(b, 0, n);
方式二
基于FileStream,一個(gè)字節(jié)一個(gè)字節(jié)讀取,放到字節(jié)數(shù)組中,最后轉(zhuǎn)換成字符串進(jìn)行顯示。
this.rtb_Content.Clear();
FileStream fs = new FileStream(this.txt_FilePath.Text, FileMode.Open, FileAccess.Read);
long n = fs.Length;
byte[] b = new byte[n];
int data, index;
index = 0;
data = fs.ReadByte();
while (data != -1)
{
b[index++] = Convert.ToByte(data);
data = fs.ReadByte();
}
fs.Close();
this.rtb_Content.Text = Encoding.UTF8.GetString(b);
方式三
基于File類,直接全部讀取出來并顯示。
this.rtb_Content.Clear();
this.rtb_Content.Text = File.ReadAllText(this.txt_FilePath.Text, Encoding.UTF8);
方式四
基于StreamReader,一行一行讀取,最后拼接并顯示。
this.rtb_Content.Clear();
StreamReader sr = new StreamReader(this.txt_FilePath.Text, Encoding.UTF8);
string line = sr.ReadLine();
while (line != null)
{
this.rtb_Content.AppendText(line);
line = sr.ReadLine();
if (line != null)
{
this.rtb_Content.AppendText("\\r\\n");
}
}
sr.Close();
方式五
基于StreamReader,一次性讀取到結(jié)尾,最后顯示。
this.rtb_Content.Clear();
StreamReader sr = new StreamReader(this.txt_FilePath.Text, Encoding.UTF8);
this.rtb_Content.Text = sr.ReadToEnd();
sr.Close();
方式六
基于StreamReader,一行一行讀取,通過EndOfSteam判斷是否到結(jié)尾,最后拼接并顯示。
this.rtb_Content.Clear();
StreamReader sr = new StreamReader(this.txt_FilePath.Text, Encoding.UTF8);
while (!sr.EndOfStream)
{
this.rtb_Content.AppendText(sr.ReadLine());
if (!sr.EndOfStream)
{
this.rtb_Content.AppendText("\\r\\n");
}
}
sr.Close();
方式7
基于FileStream和StreamReader來實(shí)現(xiàn)。
this.rtb_Content.Clear();
FileStream fs = new FileStream(this.txt_FilePath.Text, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, Encoding.UTF8);
this.rtb_Content.Text = sr.ReadToEnd();
fs.Close();
sr.Close();
測(cè)試結(jié)果
經(jīng)過測(cè)試,以上每個(gè)方法都可以實(shí)現(xiàn)文本文件的讀取。
總結(jié)
以上7種方式主要是分別基于FileStream、File和StreamReader這三種來實(shí)現(xiàn)的,這三種方式的區(qū)別在于:
- FileStream類可以對(duì)任意類型的文件進(jìn)行讀取操作,而且我們也可以按照需要指定每一次讀取字節(jié)長度,以此減少內(nèi)存的消耗,提高讀取效率。
- StreamReader的特點(diǎn)是,它只能對(duì)文本文件進(jìn)行讀寫操作,可以一行一行的寫入和讀取。
- File類它是一個(gè)靜態(tài)類,當(dāng)我們查看file類的那些靜態(tài)方法時(shí),我們可以發(fā)現(xiàn),在這個(gè)類里面的方法封裝了可以執(zhí)行文件讀寫操作的對(duì)象,例如:Filestream,StreamReader,我們通過File去執(zhí)行任何文件的讀寫操作時(shí),實(shí)際上是使用FileStream或SteamReader對(duì)象來執(zhí)行文件的讀寫操作,代碼如下所示:
public static string ReadAllText(string path, Encoding encoding)
{
if (path == null)
{
throw new ArgumentNullException("path");
}
if (encoding == null)
{
throw new ArgumentNullException("encoding");
}
if (path.Length == 0)
{
throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
}
return InternalReadAllText(path, encoding, checkHost: true);
}
private static string InternalReadAllText(string path, Encoding encoding, bool checkHost)
{
using (StreamReader streamReader = new StreamReader(path, encoding, detectEncodingFromByteOrderMarks: true, StreamReader.DefaultBufferSize, checkHost))
{
return streamReader.ReadToEnd();
}
}
-END-
-
字符串
+關(guān)注
關(guān)注
1文章
589瀏覽量
21218 -
上位機(jī)
+關(guān)注
關(guān)注
27文章
963瀏覽量
55699 -
Read
+關(guān)注
關(guān)注
0文章
10瀏覽量
11246
發(fā)布評(píng)論請(qǐng)先 登錄
《Visual C# 2008程序設(shè)計(jì)經(jīng)典案例設(shè)計(jì)與實(shí)現(xiàn)》---動(dòng)態(tài)讀取XMI文件
Python與C#對(duì)比
如何利用C#去實(shí)現(xiàn)一種HMACSHA256加密算法呢
讀取并分析GPS數(shù)據(jù)的GPSReader程序(C#和VB.net版代碼)

C語言入門教程-讀取文本文件
使用C#實(shí)現(xiàn)Arduino與電腦進(jìn)行串行通訊 - Alex Le
C#教程之調(diào)用SMTP發(fā)送文本內(nèi)容
C#教程之讀取數(shù)據(jù)庫表結(jié)構(gòu)
《Visual C# 2008程序設(shè)計(jì)經(jīng)典案例設(shè)計(jì)與實(shí)現(xiàn)》---
《Visual C# 2008程序設(shè)計(jì)經(jīng)典案例設(shè)計(jì)與實(shí)現(xiàn)》---
《Visual C# 2008程序設(shè)計(jì)經(jīng)典案例設(shè)計(jì)與實(shí)現(xiàn)》---
C#實(shí)現(xiàn)ActiveX控件開發(fā)與部署

評(píng)論