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 ...
随机推荐
- centos6.5 以 zero-dependency Erlang from RabbitMQ 搭建环境
rabbitmq 官方安装文档可参考:http://www.rabbitmq.com/install-rpm.html ,由于rabbitmq 使用Erlang 开发的,运行环境需要用到Erlang ...
- [BZOJ1034][ZJOI2008]泡泡堂BNB 贪心
1034: [ZJOI2008]泡泡堂BNB Time Limit: 10 Sec Memory Limit: 162 MB Submit: 3414 Solved: 1739 [Submit][ ...
- [python] win7 64位 安装pygame
1.下载pygame 2.python 下载3.2.* 32位的(电脑64位没关系的) 3.先安装python,再安装pygame 4.验证是否成功 打开IDLE >>>impor ...
- 牛客练习赛16 F 选值【二分/计数】
链接:https://www.nowcoder.com/acm/contest/84/F 来源:牛客网 题目描述 给定n个数,从中选出三个数,使得最大的那个减最小的那个的值小于等于d,问有多少种选法. ...
- linux中,在在shadowsoks下怎么更新软件
在shadowsoks下怎么更新软件 先描述一下我的情况.我们学校不给大一开通校园网,我自己租用了***的vps服务器,搭建shadowsocks,(使用ipv6地址才能连接),开始了自己悲催的上网生 ...
- bean装配--auto
1,Dao package com.songyan.autoZhuangpei; public interface UserDao { public void say(); } package com ...
- FIREDAC记录SQL日志
FIREDAC记录SQL日志 跟踪SQL日志可以方便开发的时候的程序调试.SQL日志记录会耗费服务费资源,正式部署中间件的时候,建议关闭SQL日志记录. FIREDAC通过使用TFDMoniFlatF ...
- android 代码覆盖率
背景 项目使用的是small插件.一个app分为main和多个插件,为了统计插件的代码覆盖率. 1 修改插件 修改插件build.gradle buildTypes { release { ... } ...
- 【spring boot】【mybatis】spring boot中mybatis打印sql语句
spring boot中mybatis打印sql语句,怎么打印出来?[参考:https://www.cnblogs.com/sxdcgaq8080/p/9100178.html] 在applicati ...
- 设计模式之工厂模式之简单工厂(php实现)
github源码地址: git@github.com:ZQCard/design_pattern.git 1.简单工厂模式 特点:将调用者与创建者分离,调用者直接向工厂请求,减少代码的耦合.提高系统的 ...