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("迪迦奥特曼会飞"); 
}
相关推荐
星光一影14 小时前
基于PHP+MySQL+Uniapp的上门家政服务系统源码
开发语言·mysql·uni-app·php
Antonio91514 小时前
【Swift】 UIKit:UIGestureRecognizer和UIView Animation
开发语言·ios·swift
D***t13114 小时前
PHP在API开发中的框架选择
开发语言·php
H***997614 小时前
Java虚拟现实案例
java·开发语言·vr
Tan_Ying_Y14 小时前
synchronized和ReentrantLock的区别是什么?他们的底层原理是什么?
开发语言·c#
gc_229914 小时前
学习C#调用Microsoft.Office.Interop.Word将Word转换为html
c#·html·word·interop.word
ChineHe14 小时前
Golang并发编程篇002_Go并发基础
开发语言·后端·golang
默恋~微凉14 小时前
shell(八)——WEB与Nginx
开发语言·前端·php
lsx20240614 小时前
Go 语言类型转换
开发语言
t***L26615 小时前
JavaScript在机器学习中的库
开发语言·javascript·机器学习