0、创建文件流几种方法:

File.Open("a.txt",FileMode.Create,FileAccess.ReadWrite);
File.OpenWrite("a.txt");

FileStream fsRead = File.OpenRead("a.txt");//默认只读

1、FileStream基本用法

            #region FileStream文件流的使用方式
//1.创建一个 中国.txt
string txt = "中国是世界上人口第一大国。中国是世界上最幸福的国家之一。";
//一、创建一个文件流
FileStream fs = new FileStream(@"c:\中国.txt", FileMode.Create, FileAccess.Write);
byte[] buffer = Encoding.UTF8.GetBytes(txt);
//二、读文件或者写文件
//参数1:表示要把哪个byte[]数组中的内容写入到文件
//参数2:表示要从该byte[]数组的第几个下标开始写入,一般都是0
//参数3:要写入的字节的个数。
fs.Write(buffer, 0, buffer.Length);
////三、关闭文件流
////清空缓冲区
//fs.Flush();
//fs.Close();
//四、释放相关资源
fs.Dispose();//自动调用close和flush方法
Console.WriteLine("ok");
Console.ReadKey();
=================================================================
//1.创建一个 中国.txt
string txt = "中国是世界上人口第一大国。中国是世界上最幸福的国家之一。中国是四大文明古国之一。中国有个杨中科。";
//一、创建一个文件流
//当把一个对象放到using()中的时候,当超出using的作用于范围后,会自动调用该对象的Dispose()f方法。
using (FileStream fs = new FileStream(@"c:\中国.txt", FileMode.Create, FileAccess.Write))
{
byte[] buffer = Encoding.UTF8.GetBytes(txt);
//二、读文件或者写文件
//参数1:表示要把哪个byte[]数组中的内容写入到文件
//参数2:表示要从该byte[]数组的第几个下标开始写入,一般都是0
//参数3:要写入的字节的个数。
fs.Write(buffer, 0, buffer.Length);
}
Console.WriteLine("ok");
Console.ReadKey();
#endregion

2、文件copy

            #region 使用文件流进行拷贝

            string source = @"e:\全面回忆.rmvb";
string target = @"c:\全面回忆.rmvb";
CopyFile(source, target);
Console.WriteLine("ok");
Console.ReadKey();
#endregion

CopyFile方法实现

        //文件拷贝
private static void CopyFile(string source, string target)
{
//1.创建一个读取源文件的文件流
using (FileStream fsRead = new FileStream(source, FileMode.Open, FileAccess.Read))
{
//获取原文件的大小
long len = fsRead.Length;
//2.创建一个写入新文件的文件流
using (FileStream fsWrite = new FileStream(target, FileMode.Create, FileAccess.Write))
{ //创建缓冲区
byte[] buffer = new byte[1024 * 1024 * 5];//更方便阅读5M //3.通过fsRead读取源文件,然后再通过fsWrite写入新文件 //通过文件流读取
//参数1:表示将来读取到的内容要存放到哪个数组中
//参数2:表示这个数据要从第几个索引开始填充数据、
//参数3:表示本次读取最多可以读取多少个字节。
//返回值:表示本次实际读取到的字节个数。
int byteCount = fsRead.Read(buffer, 0, buffer.Length);
while (byteCount > 0)
{
//把刚刚读取到的内容写入到新文件流中
fsWrite.Write(buffer, 0,byteCount);
double d = fsWrite.Position * 1.0 / len;
//需要循环执行读写操作 //把上次读取到内容写入完毕后,继续再读取
byteCount = fsRead.Read(buffer, 0, buffer.Length);
//fsRead.Position
//fsWrite.Position
Console.WriteLine("已经拷贝完毕了:{0}%", d * 100);
}
}
}
}

 3、文件加密方法  

        private void CopyEncryptFile(string source, string target)
{
//读
using (FileStream fsRead = new FileStream(source, FileMode.Open, FileAccess.Read))
{
//写
using (FileStream fsWrite = new FileStream(target, FileMode.Create, FileAccess.Write))
{
//缓冲区
byte[] buffer = new byte[1024 * 1024 * 1];
int byteCount = fsRead.Read(buffer, 0, buffer.Length);
//这个应该写在循环里面
//========================加密==================================
for (int i = 0; i < byteCount; i++)
{
buffer[i] = (byte)(byte.MaxValue - buffer[i]);
}
//========================加密==================================
while (byteCount > 0)
{
fsWrite.Write(buffer, 0, byteCount);
byteCount = fsRead.Read(buffer, 0, buffer.Length);
}
}
}
}

  

 

c#Filestream类(文件流)的更多相关文章

  1. FileStream读写文件流

    用FileStream 读取文件流并显示给文件内容 string p = @"C:\Users\Administrator\Desktop\1.txt"; FileStream f ...

  2. .net学习之集合、foreach原理、Hashtable、Path类、File类、Directory类、文件流FileStream类、压缩流GZipStream、拷贝大文件、序列化和反序列化

    1.集合(1)ArrayList内部存储数据的是一个object数组,创建这个类的对象的时候,这个对象里的数组的长度为0(2)调用Add方法加元素的时候,如果第一次增加元神,就会将数组的长度变为4往里 ...

  3. FileStream类的使用(文件流)

    1.什么是FileStream类 FileStream 类对文件系统上的文件进行读取.写入.打开和关闭操作,并对其他与文件相关的操作系统句柄进行操作,如管道.标准输入和标准输出.读写操作可以指定为同步 ...

  4. 流(Stream)与文件流(FileStream)

    //通过流的方式添加 StreamWriter writer = new StreamWriter(@"C:\A\ca.txt", true, Encoding.Default); ...

  5. 文件读写(三)利用FileStream类操作字节数组byte[]、BinaryFormatter、内存流MemoryStream

    一.Stream类概述 在.NET Framework中,文件和流是有区别的.文件是存储在磁盘上的数据集,它具有名称和相应的路径.当打开一个文件并对其进行读/写时,该文件就称为流(stream).但是 ...

  6. 02-大文件Copy(FileStream文件流类)

    static void Main(string[] args) { string source = @"e:\1.exe";//要移动文件的路径 大文件 string target ...

  7. C# Path类 FileStream(文件流) 与 File(文件) 读取的区别

    1.采用文件流读取数据是一点一点从文件中读取数据对内存的压力相对较小;而采用文件读取数据是一下全部读取过来对内存造成的压力相对较大 2.File读取: string str = @"E:\Q ...

  8. 文件夹和文件、Path类、流、序列化

    循环访问目录树 参考: http://msdn.microsoft.com/zh-cn/library/bb513869.aspx 循环访问目录树”的意思是在指定的根文件夹下,访问每个嵌套子目录中任意 ...

  9. 文件流FileStram类

    本节课主要学习三个内容: 创建FileStram流 读取流 写入流 文件流FileStram类,是用来实现对文件的读取和写入.FileStram是操作字节的字节数组,当提供向文件读取和写入字节的方法时 ...

随机推荐

  1. 各大主流.Net的IOC框架性能测试比较(转)

    出处:http://www.cnblogs.com/liping13599168/archive/2011/07/17/2108734.html 在上一篇中,我简单介绍了下Autofac的使用,有人希 ...

  2. window.location 对象

    http://www.home.com:8080/windows/location/page.html?ver=1.0&id=timlq#love 1, window.location.hre ...

  3. Linux 基础教程 42-xargs命令

        xargs是execute arguments的缩写,主要作用是从标准输入中读取内容,并将此内容传递给它要协助的命令,并作为要协助命令的参数来执行. 基本语法 xargs [选项] [命令] ...

  4. MySQL—练习

    前面学习了MySQL的语句的基本用法,这里就开始做一些MySQL练习,这套题目一共45题,属于比较简单的,初学先试着做这个. 参考链接:https://www.cnblogs.com/SJP666/p ...

  5. View Pi's Status on WebBrowser

    1. install php and cgi support sudo apt-get install php5-common sudo apt-get install php5-cgi sudo a ...

  6. IDEA配置spring

    大半天都在看spring,以前总是看不下去,这次慢慢来,慢慢看. 看那些基础的,倒是还不错.好多都是关于helloworld的,写完helloworld,觉得不怎么形象.于是写了动物,作为接口. (1 ...

  7. CVE-2018-7600 Drupal核心远程代码执行漏洞分析

    0x01 漏洞介绍 Drupal是一个开源内容管理系统(CMS),全球超过100万个网站(包括政府,电子零售,企业组织,金融机构等)使用.两周前,Drupal安全团队披露了一个非常关键的漏洞,编号CV ...

  8. bootstrap table 的searchParam参数传递

    bootstrap table 的searchParam自定义参数传递 Bootstrap Table返回的数据为value 和 rows Long total代表的是多少条(总数)  List< ...

  9. form 认证 读取

    class Program { public static CookieContainer cc { get; set; } static void Main(string[] args) { str ...

  10. Oracle.DataAccess.dll方式操作oracle数据库

    Oracle.DataAccess.dll方式操作oracle数据库 一.查询语句: using (OracleConnection conn = new OracleConnection(Syste ...