前言
文本讀取在上位機開發中經常會使用到,實現的方式也有很多種,今天跟大家分享一下C#實現讀取讀取的7種方式。
這里我們先寫好了一個測試界面,提供一個文件路徑選擇的入口,具體如下所示:
方式一
基于FileStream,并結合它的Read方法讀取指定的字節數組,最后轉換成字符串進行顯示。
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,一個字節一個字節讀取,放到字節數組中,最后轉換成字符串進行顯示。
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,一次性讀取到結尾,最后顯示。
this.rtb_Content.Clear();
StreamReader sr = new StreamReader(this.txt_FilePath.Text, Encoding.UTF8);
this.rtb_Content.Text = sr.ReadToEnd();
sr.Close();
方式六
基于StreamReader,一行一行讀取,通過EndOfSteam判斷是否到結尾,最后拼接并顯示。
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來實現。
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();
測試結果
經過測試,以上每個方法都可以實現文本文件的讀取。
總結
以上7種方式主要是分別基于FileStream、File和StreamReader這三種來實現的,這三種方式的區別在于:
- FileStream類可以對任意類型的文件進行讀取操作,而且我們也可以按照需要指定每一次讀取字節長度,以此減少內存的消耗,提高讀取效率。
- StreamReader的特點是,它只能對文本文件進行讀寫操作,可以一行一行的寫入和讀取。
- File類它是一個靜態類,當我們查看file類的那些靜態方法時,我們可以發現,在這個類里面的方法封裝了可以執行文件讀寫操作的對象,例如:Filestream,StreamReader,我們通過File去執行任何文件的讀寫操作時,實際上是使用FileStream或SteamReader對象來執行文件的讀寫操作,代碼如下所示:
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-
-
字符串
+關注
關注
1文章
589瀏覽量
20904 -
上位機
+關注
關注
27文章
952瀏覽量
55428 -
Read
+關注
關注
0文章
10瀏覽量
11199
發布評論請先 登錄
相關推薦
《Visual C# 2008程序設計經典案例設計與實現》---動態讀取XMI文件
Python與C#對比
C語言入門教程-讀取文本文件
《Visual C# 2008程序設計經典案例設計與實現》---
C#實現ActiveX控件開發與部署

評論