【讀寫txt文件】
電腦手機上有各種各樣的文件,例如視頻文件、圖片文件、文本文件,其中讀寫txt文件是最簡單的,有多種方式,
使用StreamReader和StreamWriter
//讀取文件
string path = @"C:\example.txt"; // 文件路徑
using (StreamReader reader = new StreamReader(path))//使用using語句來確保資源被正確釋放,以避免資源泄漏
{
string line;
while ((line = reader.ReadLine()) != null) // 逐行讀取文件內容,每次讀取一行,讀取到末尾的時候為空
{
Console.WriteLine(line); // 輸出每行內容到控制臺
}
}
//寫入文件
string path = @"C:\example.txt"; // 文件路徑
using (StreamWriter writer = new StreamWriter(path))
{
string content = "Hello, World!"; // 要寫入文件的內容
writer.WriteLine(content); // 寫入一行內容到文件
}
使用TextReader和TextWriter?
//讀取文件
string path = @"C:\example.txt"; // 文件路徑
using (TextReader reader = new StreamReader(path))
{
string line;
while ((line = reader.ReadLine()) != null) // 逐行讀取文件內容
{
Console.WriteLine(line); // 輸出每行內容到控制臺
}
}
//寫入文件
string path = @"C:\example.txt"; // 文件路徑
using (TextReader reader = new StreamReader(path))
{
string line;
while ((line = reader.ReadLine()) != null) // 逐行讀取文件內容
{
Console.WriteLine(line); // 輸出每行內容到控制臺
}
}
?使用FileStream
//讀取文件
string path = @"C:\example.txt"; // 文件路徑
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
using (StreamReader reader = new StreamReader(fs))
{
string line;
while ((line = reader.ReadLine()) != null) // 逐行讀取文件內容
{
Console.WriteLine(line); // 輸出每行內容到控制臺
}
}
}
//寫入文件
string path = @"C:\example.txt"; // 文件路徑
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
{
using (StreamWriter writer = new StreamWriter(fs))
{
string content = "Hello, World!"; // 要寫入文件的內容
writer.WriteLine(content); // 寫入一行內容到文件
}
}
使用File類提供的靜態(tài)方法
上面幾種方法代碼都很長,一般來說我們幾乎不會使用這些方法,使用File類提供的靜態(tài)方法更便捷一些。如果文件非常大,還是要用上面的方法讀文件,否則一次性讀進來,內存會爆。
如果期望追加寫入,還是要用上面的方法,StreamWriter的參數設置為true就好,例如StreamWriter sw = new StreamWriter(filePath, true);
//讀取文件
string path = @"C:\example.txt"; // 文件路徑
string content = File.ReadAllText(path); // 讀取整個文件內容
Console.WriteLine(content); // 輸出文件內容到控制臺
//寫入文件
string path = @"C:\example.txt"; // 文件路徑
string content = "Hello, World!"; // 要寫入文件的內容
File.WriteAllText(path, content); // 將內容寫入文件
//上面是一次性讀取寫入,得到的對象是一個非常大的string,但有時我們需要逐行處理,就要逐行讀取寫入
string path = @"C:\example.txt"; // 文件路徑
string[] lines = File.ReadAllLines(path); // 讀取所有行
foreach (string line in lines)
{
Console.WriteLine(line); // 在控制臺輸出每一行
}
string path = @"C:\example.txt"; // 文件路徑
string[] lines = { "第一行內容", "第二行內容", "第三行內容" }; // 字符串數組,每個元素將成為一行
File.WriteAllLines(path, lines); // 將字符串數組中的所有元素寫入到文件中
文本編碼
如果讀出來的內容是亂碼,建議了解文本編碼,在讀取和寫入文本時都可以傳遞文本編碼參數。
【讀寫二進制文件】?
使用BinaryWriter和BinaryReader
我們知道在磁盤上只會存儲二進制數據,文本文件最后也會被保存為二進制文件,我們調用接口讀取和寫入時雖然用的是string,但到底層一定是byte[]。這就涉及到string到byte[]的編碼和byte[]到string的解碼,只不過對于文本文件而言,有確定的編碼解碼規(guī)則,我們不需要關心。
而讀寫二進制文件,需要你自己設置編碼解碼規(guī)則。編碼解碼的類型不止是string,可能是各種類型的。
//寫入文件
using (BinaryWriter writer = new BinaryWriter(File.Open("file.bytes", FileMode.Create)))
{
bool boolValue = true;
writer.Write(boolValue);
int intValue = 123;
writer.Write(intValue);
float floatValue = 3.14f;
writer.Write(floatValue);
double doubleValue = 3.1415926;
writer.Write(doubleValue);
char charValue = 'A';
writer.Write(charValue);
string content = "永恒之星";
byte[] bytes = Encoding.UTF8.GetBytes(content);
writer.Write(bytes.Length); // 寫入字符串長度
writer.Write(bytes); // 寫入字符串字節(jié)數組
}
?讀取文件時按照寫入的順序一個個讀取即可。
//讀取文件
using (BinaryReader reader = new BinaryReader(File.Open("file.bytes", FileMode.Open)))
{
bool boolValue = reader.ReadBoolean();
Debug.Log(boolValue);
int intValue = reader.ReadInt32();
Debug.Log(intValue);
float floatValue = reader.ReadSingle();
Debug.Log(floatValue);
double doubleValue = reader.ReadDouble();
Debug.Log(doubleValue);
char charValue = reader.ReadChar();
Debug.Log(charValue);
int length = reader.ReadInt32();
byte[] bytes = reader.ReadBytes(length);
string content = Encoding.UTF8.GetString(bytes);
Debug.Log(content);
}
使用MemoryStream
上面的問題在于是一次次讀寫的,會有多次IO開銷,可以借助MemoryStream將文件內容先一次性讀寫到內存中,然后再讀寫到磁盤中。
另外,我們讀寫數據時是一行行代碼讀寫的,如果有一萬個數據,當然不可能寫一萬行代碼,所以對同類型的數據需要將數據個數也寫入,用for循環(huán)減少代碼量。
//寫入文件
using (MemoryStream ms = new MemoryStream())
{
using (BinaryWriter writer = new BinaryWriter(ms))
{
bool boolValue = true;
writer.Write(boolValue);
int count = 5;
writer.Write(count);
for (int i = 0; i < count; i++)
{
int intValue = 123;
writer.Write(intValue);
}
float floatValue = 3.14f;
writer.Write(floatValue);
double doubleValue = 3.1415926;
writer.Write(doubleValue);
char charValue = 'A';
writer.Write(charValue);
string content = "永恒之星";
byte[] bytes = Encoding.UTF8.GetBytes(content);
writer.Write(bytes.Length); // 寫入字符串長度
writer.Write(bytes); // 寫入字符串字節(jié)數組
}
File.WriteAllBytes("file.bytes", ms.ToArray());//一次性寫入磁盤
}
//讀取文件
using (MemoryStream ms = new MemoryStream(File.ReadAllBytes("file.bytes")))//一次性從磁盤讀取數據
{
using (BinaryReader reader = new BinaryReader(ms))
{
bool boolValue = reader.ReadBoolean();
Debug.Log(boolValue);
int count = reader.ReadInt32();
for (int i = 0; i < count; i++)
{
int intValue = reader.ReadInt32();
Debug.Log(intValue);
}
float floatValue = reader.ReadSingle();
Debug.Log(floatValue);
double doubleValue = reader.ReadDouble();
Debug.Log(doubleValue);
char charValue = reader.ReadChar();
Debug.Log(charValue);
int length = reader.ReadInt32();
byte[] bytes = reader.ReadBytes(length);
string content = Encoding.UTF8.GetString(bytes);
Debug.Log(content);
}
}
拓展文章來源:http://www.zghlxwxcb.cn/news/detail-740880.html
如果我們有很多不同類型的數據需要寫到不同的二進制文件中,而且后期的數據結構還可能會修改,那么每次自己讀寫就非常繁瑣,維護成本很高。我們需要借助些序列化工具幫忙讀寫,我們只需要定義好數據結構,并填充數據即可。這些工具有BinaryFomatter、ProtoBuf、FlatBuffer,更具需求選擇合理的工具。文章來源地址http://www.zghlxwxcb.cn/news/detail-740880.html
到了這里,關于C# 流Stream詳解(1)——讀寫txt和二進制文件的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!