C#中FileStream和StreamWriter/StreamReader的区别
其中创建FileStream对象最简单的构造函数如下:
FileStream file = new FileStream(fileName,FileMode.Member);//默认方式,可读可写
FileStream file = new FileStream(fileName, FileMode.Member, FileAccess.Member);
而FileAccess的成员:
成 员 |
说 明 |
Read |
打开文件,用于只读 |
Write |
打开文件,用于只写 |
ReadWrite |
打开文件,用于读写 |
对文件进行不是FileAccess枚举成员指定的操作会导致抛出异常。此属性的作用是,基于用户的身份验证级别改变用户对文件的访问权限。
在FileStream构造函数不使用FileAccess枚举参数的版本中,使用默认值FileAccess. ReadWrite。
FileMode枚举成员,使用每个值会发生什么,取决于指定的文件名是否表示已有的文件。
成 员 |
文 件 存 在 |
文件不存在 |
Append |
打开文件,流指向文件的末尾,只能与枚举FileAccess.Write联合使用 |
创建一个新文件。只能与枚举FileAccess.Write联合使用 |
Create |
删除该文件,然后创建新文件 |
创建新文件 |
CreateNew |
抛出异常 |
创建新文件 |
Open |
打开现有的文件,流指向文件的开头 |
抛出异常 |
OpenOrCreate |
打开文件,流指向文件的开头 |
创建新文件 |
Truncate |
打开现有文件,清除其内容。流指向文件的开头,保留文件的初始创建日期 |
抛出异常 |
FileStream类操作的是字节和字节数组,而Stream类操作的是字符数据
StreamWriter允许将字符和字符串写入文件,它处理底层的转换,向FileStream对象写入数据。StreamReader也类似。
实例:
using System;
using System.Data;
using System.IO;
using System.Text;
/// Summary description for FileReadAndWrite
public class FileReadAndWrite
{
public FileReadAndWrite()
{
// TODO: Add constructor logic here
}
/// 用FileStream写文件
public void FileStreamWriteFile(string str)
{
byte[] byData;
char[] charData;
try
{
FileStream nFile = new FileStream("love.txt", FileMode.Create);
//获得字符数组
charData = str.ToCharArray();
//初始化字节数组
byData = new byte[charData.Length];
//将字符数组转换为正确的字节格式
Encoder enc = Encoding.UTF8.GetEncoder();
enc.GetBytes(charData, 0, charData.Length,byData,0,true);
nFile.Seek(0, SeekOrigin.Begin);
nFile.Write(byData, 0, byData.Length);
}
catch (Exception ex)
{
throw ex;
}
}
/// FileStream读取文件
public string FileStreamReadFile(string filePath)
{
byte[] data = new byte[100];
char[] charData = new char[100];
try
{
FileStream file = new FileStream(filePath, FileMode.Open);
//文件指针指向0位置
file.Seek(0, SeekOrigin.Begin);
//读入两百个字节
file.Read(data, 0, 200);
//提取字节数组
Decoder dec = Encoding.UTF8.GetDecoder();
dec.GetChars(data, 0, data.Length, charData, 0);
}
catch (Exception ex)
{
throw ex;
}
return Convert.ToString(charData);
}
/// StreamWriter写文件
public void StreamWriterWriteFile()
{
try
{
FileStream nFile = new FileStream("love.txt", FileMode.CreateNew);
StreamWriter writer = new StreamWriter(nFile);
writer.WriteLine("I love You!");
writer.WriteLine("Do you love me!");
writer.Close();
}
catch
{ }
}
/// StreamReader读取文件
public string StreamReaderReadFile()
{
string str="";
try
{
FileStream file = new FileStream("love.txt", FileMode.Open);
StreamReader sr = new StreamReader(file);
while (sr.ReadLine()!=null)
{
str += sr.ReadLine();
}
//或者str = sr.ReadToEnd();
sr.Close();
}
catch
{ }
return str;
}
}
C#中FileStream和StreamWriter/StreamReader的区别的更多相关文章
- C#文件与流(FileStream、StreamWriter 、StreamReader 、File、FileInfo、Directory、directoryInfo、Path、Encoding)
(FileStream.StreamWriter .StreamReader .File.FileInfo.Directory.DirectoryInfo.Path.Encoding) C#文 ...
- FileStream StreamWriter StreamReader BinaryReader
FileStream vs/differences StreamWriter? http://stackoverflow.com/questions/4963667/filestream-vs-dif ...
- C#中FileStream的对比以及使用方法
场景 File与FileStream的区别 举例: 将读取文件比作是从A桶往B桶运水. 使用File就是整个用桶倒进去,使用FileStream就是使用水管慢慢输送. FileStream与Strea ...
- File、FileStream、StreamWriter、StringWriter文件使用总结
一.File 1.File为静态类 File类,是一个静态类,支持对文件的基本操作,包括创建,拷贝,移动,删除和打开一个文件.File类方法的参量很多时候都是路径path.主要提供有关文件的各种操作, ...
- JDBC中的Statement和PreparedStatement的区别
JDBC中的Statement和PreparedStatement的区别
- LINQ语句中的.AsEnumerable() 和 .AsQueryable()的区别
LINQ语句中的.AsEnumerable() 和 .AsQueryable()的区别 在写LINQ语句的时候,往往会看到.AsEnumerable() 和 .AsQueryable() .例如: s ...
- JavaScript中const、var和let区别浅析
在JavaScript中有三种声明变量的方式:var.let.const.下文给大家介绍js中三种定义变量的方式const, var, let的区别. 1.const定义的变量不可以修改,而且必须初始 ...
- 【前端】js中new和Object.create()的区别
js中new和Object.create()的区别 var Parent = function (id) { this.id = id this.classname = 'Parent' } Pare ...
- SQL Server中Text和varchar(max)数据类型区别
SQL Server中Text和varchar(max)数据类型区别 以前只知道text和image是可能被SQL Server淘汰的数据类型,但具体原因不太清楚,今天读书的时候发现了text与v ...
随机推荐
- HDU 2602.Bone Collector-动态规划0-1背包
Bone Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- CF987C Three displays【一维DP/类似最大子序列和】
[链接]:CF987C [分析]:先求出每个s[i]后面比s[i]大的c[i]的最小值,然后枚举前两个数c(i),c(j)以及 j 后面递增且存在最小值的dp(j) [代码]: #include< ...
- struts2进阶
Struts2 一.Struts的工作原理 Struts2的工作机制3.1Struts2体系结构图 Strut2的体系结构如图15所示: (图15) 3.2Struts2的工作机制 从图15可以看出, ...
- Python与正则表达式[0] -> re 模块的正则表达式匹配
正则表达式 / Regular Expression 目录 正则表达式模式 re 模块简介 使用正则表达式进行匹配 正则表达式RE(Regular Expression, Regexp, Regex) ...
- Python的并发并行[1] -> 线程[0] -> threading 模块
threading模块 / threading Module 1 常量 / Constants Pass 2 函数 / Function 2.1 setprofile()函数 函数调用: thread ...
- Python的程序结构[1] -> 方法/Method[3] -> 魔术方法 __getattr__ 与代理模式
__getattr__ 方法 __getattr__ 方法当对象调用内部属性(包括方法等)且未找到对应属性的时候会调用的特殊方法.利用这一特性,可是对函数实现一个代理模式. __getattr__方法 ...
- 状压DP【p1879】[USACO06NOV]玉米田Corn Fields
Description 农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地.John打算在牧场上的某几格里种上 ...
- [CTSC2018]假面(概率DP)
考场上以为CTSC的概率期望题都不可做,连暴力都没写直接爆零. 结果出来发现全场70以上,大部分AC,少于70的好像极少,感觉血亏. 设a[i][j]表示到当前为止第i个人的血量为j的概率(注意特判血 ...
- DB Link
oracle中DB Link select * from TB_APP_HEADER@SSDPPORTAL
- exports 与 module.exports 的区别
exports与module.exports的作用就是将方法或者是变量暴露出去,以便给其他模块调用,再直接点,就是给其他模块通过require()的方式引用. 那么require()一个模块时,到底做 ...