C#——文件读取StreamRead和StreamWriter类详情

文件读取StreamRead和StreamWriter类

StreamReader 用于从字节流中读取字符StreamWriter 用于向一个流中写入字符

使用

读取

cs 复制代码
// using 使用,语句可以省去关闭StreamReader读写流
using (StreamReader sr = new StreamReader(@"1.txt", Encoding.UTF8))
{ 
        string lines; 
        // sr.ReadLine() 读取数据 如果读到文件的末尾时候,不再有数据,该方法的返回值为null
        while ((lines = sr.ReadLine()) != null)
        {
                Console.WriteLine(lines);
        }
}

写入

cs 复制代码
using(StreamWriter sw = new StreamWriter(@"2.txt")) 
{ 
sw.WriteLine("迪迦奥特曼会飞"); 
}
相关推荐
Tony Bai6 小时前
高并发后端:坚守 Go,还是拥抱 Rust?
开发语言·后端·golang·rust
wjs20246 小时前
Swift 类型转换
开发语言
秃了也弱了。7 小时前
python实现定时任务:schedule库、APScheduler库
开发语言·python
weixin_440730507 小时前
java数组整理笔记
java·开发语言·笔记
Thera7777 小时前
状态机(State Machine)详解:原理、优缺点与 C++ 实战示例
开发语言·c++
niucloud-admin8 小时前
java服务端——controller控制器
java·开发语言
夏幻灵8 小时前
JAVA基础:基本数据类型和引用数据类型
java·开发语言
cike_y9 小时前
Spring-Bean的作用域&Bean的自动装配
java·开发语言·数据库·spring
十八度的天空9 小时前
第01节 Python的基础语法
开发语言·python
我是唐青枫9 小时前
深入理解 C#.NET Interlocked.Increment:原子操作的核心
c#·.net