FileStream类属性和方法
属性
CanRead 指示当前文件流是否支持读取
CanWrite 指示当前文件流是否支持写入
CanSeek 指示当前文件流是否支持查找
IsAsync FileStream是同步打开还是异步打开
Length 流的长度(字节数)
CanTimeOut 当前文件流是否可以超时
ReadTimeOut 最大读取时间,超过则超时
WriteTimeOut 最大写入时间,超过则超时
方法
Read() 从文件中读取字节块
ReadByte() 从文件中读取一个字节
Write() 将字节块写入文件
WriteByte() 将一个字节写入文件
Seek() 设置当前流的位置
Close() 关闭当前流并释放与之关联的资源
Dispose(): 释放由 Stream 使用的所有资源
FileStream同样使用命名空间System.IO
将创建文件流对象的过程写在using当中,会自动的帮助我们释放流所占用的资源。
FileStream类相关的枚举
FileMode:Append、Create、CreateNew、Open、OpenOrCreate、Truncate(切除)
FileAccess:Read、ReadWrite、Write
FileShare:Inheritable(使文件的句柄可由子进程进行继承)、None、Read、ReadWrite、Write
写入文件:
public static string filePath = @"F:codeFileStreams.txt";
public FileStream ff = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
public string sss = "";
/// <summary>
/// 创建文件并写入
/// </summary>
private void button1_Click(object sender, EventArgs e)
{
string filePath = @"F:codeFileStreams.txt";
FileStream ff = new FileStream(filePath,FileMode.OpenOrCreate,FileAccess.ReadWrite);
string content = @richTextBox1.Text;
// 将字符串读入字节数组中。
byte[] buffer = Encoding.Default.GetBytes(content);
// 将数组写入文件
ff.Write(buffer, 0, buffer.Length);
ff.Close();
MessageBox.Show("写入成功");
}
文件读取:
/// <summary>
/// 读取文件
/// </summary>
private void button2_Click(object sender, EventArgs e)
{
if (sss == "")
{
byte[] buffer = new byte[1024 * 1024 * 2]; //定义一个2M的字节数组
//返回本次实际读取到的有效字节数
int r = ff.Read(buffer, 0, buffer.Length); //每次读取2M放到字节数组里面
//将字节数组中每一个元素按照指定的编码格式解码成字符串
sss = Encoding.Default.GetString(buffer, 0, r);
label1.Text = sss;
// 关闭文件流
ff.Close();
// 关闭资源
ff.Dispose();
}
else
{
MessageBox.Show("请不要重复点击");
}
}
复制文件:
/// <summary>
/// 使用FileStream复制文件
/// </summary>
private void button3_Click(object sender, EventArgs e)
{
string oldPath = @"F:视频教程曾瑛C#视频教程合集(111课程)zy1.flv";
string newPath = @"D:qqqqqqq.flv";
CopyFile(oldPath, newPath);
MessageBox.Show("复制成功");
}
/// <summary>
/// 自定义文件复制函数
/// </summary>
public static void CopyFile(string source,string target)
{
//创建负责读取的流
using (FileStream fsread = new FileStream(source, FileMode.Open, FileAccess.Read))
{
//创建一个负责写入的流
using (FileStream fswrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
{
byte[] buffer=new byte[1024*1024*5]; //声明一个5M大小的字节数组
//因为文件不止5M,要循环读取
while(true)
{
int r=fsread.Read(buffer, 0, buffer.Length); //返回本次实际读取到的字节数
//如果返回一个0时,也就意味着什么都没有读到,读取完了
if(r==0)
break;
fswrite.Write(buffer,0,r);
}
}
}
}
测试使用全部代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace FileStreams
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string filePath = @"F:codeFileStreams.txt";
public FileStream ff = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
public string sss = "";
/// <summary>
/// 创建文件并写入
/// </summary>
private void button1_Click(object sender, EventArgs e)
{
string filePath = @"F:codeFileStreams.txt";
FileStream ff = new FileStream(filePath,FileMode.OpenOrCreate,FileAccess.ReadWrite);
string content = @richTextBox1.Text;
// 将字符串读入字节数组中。
byte[] buffer = Encoding.Default.GetBytes(content);
// 将数组写入文件
ff.Write(buffer, 0, buffer.Length);
ff.Close();
MessageBox.Show("写入成功");
}
/// <summary>
/// 读取文件
/// </summary>
private void button2_Click(object sender, EventArgs e)
{
if (sss == "")
{
byte[] buffer = new byte[1024 * 1024 * 2]; //定义一个2M的字节数组
//返回本次实际读取到的有效字节数
int r = ff.Read(buffer, 0, buffer.Length); //每次读取2M放到字节数组里面
//将字节数组中每一个元素按照指定的编码格式解码成字符串
sss = Encoding.Default.GetString(buffer, 0, r);
label1.Text = sss;
// 关闭文件流
ff.Close();
// 关闭资源
ff.Dispose();
}
else
{
MessageBox.Show("请不要重复点击");
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 使用FileStream复制文件
/// </summary>
private void button3_Click(object sender, EventArgs e)
{
string oldPath = @"F:视频教程曾瑛C#视频教程合集(111课程)zy1.flv";
string newPath = @"D:qqqqqqq.flv";
CopyFile(oldPath, newPath);
MessageBox.Show("复制成功");
}
/// <summary>
/// 自定义文件复制函数
/// </summary>
public static void CopyFile(string source,string target)
{
//创建负责读取的流
using (FileStream fsread = new FileStream(source, FileMode.Open, FileAccess.Read))
{
//创建一个负责写入的流
using (FileStream fswrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
{
byte[] buffer=new byte[1024*1024*5]; //声明一个5M大小的字节数组
//因为文件不止5M,要循环读取
while(true)
{
int r=fsread.Read(buffer, 0, buffer.Length); //返回本次实际读取到的字节数
//如果返回一个0时,也就意味着什么都没有读到,读取完了
if(r==0)
break;
fswrite.Write(buffer,0,r);
}
}
}
}
}
}
Form1.designer.cs
namespace FileStreams
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.button2 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(12, 102);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(122, 35);
this.button1.TabIndex = 0;
this.button1.Text = "创建文件并写入";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(13, 13);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(121, 83);
this.richTextBox1.TabIndex = 1;
this.richTextBox1.Text = "";
//
// button2
//
this.button2.Location = new System.Drawing.Point(13, 159);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(121, 33);
this.button2.TabIndex = 2;
this.button2.Text = "读取文件";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(13, 212);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(65, 12);
this.label1.TabIndex = 3;
this.label1.Text = "未读取文件";
//
// button3
//
this.button3.Location = new System.Drawing.Point(12, 257);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(122, 31);
this.button3.TabIndex = 4;
this.button3.Text = "文件复制";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(779, 453);
this.Controls.Add(this.button3);
this.Controls.Add(this.label1);
this.Controls.Add(this.button2);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button3;
}
}
有好的建议,请在下方输入你的评论。文章来源:https://www.toymoban.com/news/detail-526588.html
文章来源地址https://www.toymoban.com/news/detail-526588.html
到了这里,关于C#(四十六)之基于流的文件操作(FileStream)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!