網(wǎng)站首頁 編程語言 正文
StreamReader 類 (System.IO) | Microsoft 官方文檔
StreamWriter 類 (System.IO) | Microsoft 官方文檔
一、文本讀寫類:
TextReader/TextWriter:文本讀寫,抽象類
1、TextReader文本讀,其派生類:
- StreamReader:以一種特定的編碼從字節(jié)流中讀取字符。
- StringReader:從字符串讀取。
2、TextWriter文本寫,其派生類:
- StreamWriter:以一種特定的編碼向流中寫入字符。
- StringWriter:將信息寫入字符串, 該信息存儲在基礎(chǔ) StringBuilder 中。
- IndentedTextWriter:提供可根據(jù) Tab 字符串標(biāo)記縮進(jìn)新行的文本編寫器。
- HttpWriter:提供通過內(nèi)部 TextWriter 對象訪問的 HttpResponse 對象。
- HtmlTextWriter:將標(biāo)記字符和文本寫入 ASP.NET 服務(wù)器控件輸出流。 此類提供 ASP.NET 服務(wù)器控件在向客戶端呈現(xiàn)標(biāo)記時使用的格式化功能。
二、StreamReader類,讀文件
1、實例:
構(gòu)造函數(shù):默認(rèn)編碼為UTF-8
StreamReader srAsciiFromFile = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII);
StreamReader srAsciiFromStream = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),System.Text.Encoding.ASCII);
1、從文件讀取文本 Read(),Peek()
using (StreamReader sr = new StreamReader(path))
{
while (sr.Peek() >= 0)
{
Console.Write((char)sr.Read());
}
}
2、調(diào)用其ReadAsync()方法以異步方式讀取文件。
static async Task Main()
{
await ReadAndDisplayFilesAsync();
}
static async Task ReadAndDisplayFilesAsync()
{
String filename = "C:\\s.xml";
Char[] buffer;
using (var sr = new StreamReader(filename))
{
buffer = new Char[(int)sr.BaseStream.Length];
await sr.ReadAsync(buffer, 0, (int)sr.BaseStream.Length);
}
Console.WriteLine(new String(buffer));
}
3、讀取一行字符。ReadLine()
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
string line;
// Read and display lines from the file until the end of the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
4、讀取到一個操作中的文件的末尾。ReadToEnd()
using (StreamReader sr = new StreamReader(path))
{
Console.WriteLine(sr.ReadToEnd());
}
三、StreamWriter類,寫文件
實例:
StreamWriter類允許直接將字符和字符串寫入文件
//保留文件現(xiàn)有數(shù)據(jù),以追加寫入的方式打開d:\file.txt文件
using (StreamWriter sw = new StreamWriter(@"d:\file.txt", true)) //true 表示追加
{
//向文件寫入新字符串,并關(guān)閉StreamWriter
sw.WriteLine("Another File Operation Method");
}
原文鏈接:https://www.cnblogs.com/springsnow/p/9428704.html
相關(guān)推薦
- 2022-03-04 element-ui修改tooltip的背景顏色和小箭頭的顏色
- 2022-07-21 C語言全面細(xì)致講解單雙精度float與double的使用方法_C 語言
- 2022-05-08 ASP.NET?MVC擴(kuò)展HtmlHelper方法_實用技巧
- 2022-09-01 深入了解Golang的指針用法_Golang
- 2022-04-09 IDEA中命令行安裝git報錯:error: failed to push some refs to
- 2024-01-10 CloneNotSupportedException的解決方案 + Object的clone方法分析
- 2022-05-09 Python的Pandas時序數(shù)據(jù)詳解_python
- 2023-01-07 Python個人博客程序開發(fā)實例信息顯示_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支