C#(四十六)之基于流的文件操作(FileStream)

news/2024/11/16 6:49:40/

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.csnamespace 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();}#endregionprivate 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;}
}

有好的建议,请在下方输入你的评论。

在这里插入图片描述


http://www.ppmy.cn/news/724913.html

相关文章

【github】Github内置Visual Studio Code

打开内置VScode 1s.dev https://github1s.dev 对比 原始 https://github.com/vuejs/vue 内置Vscode https://github1s.dev/vuejs/vue

【RPC】—Protobuf入门

Protobuf入门 ⭐⭐⭐⭐⭐⭐ Github主页&#x1f449;https://github.com/A-BigTree 笔记链接&#x1f449;https://github.com/A-BigTree/Code_Learning ⭐⭐⭐⭐⭐⭐ Spring专栏&#x1f449;https://blog.csdn.net/weixin_53580595/category_12279588.html SpringMVC专栏&a…

二叉树刷题总结

题单&#xff1a; 一&#xff0c;相同的树 题目&#xff1a; 给你两棵二叉树的根节点 p 和 q &#xff0c;编写一个函数来检验这两棵树是否相同。 如果两个树在结构上相同&#xff0c;并且节点具有相同的值&#xff0c;则认为它们是相同的。 题目接口&#xff1a; /*** Defin…

Linux——进程信号(上)

目录 前文 一&#xff0c;什么是进程信号 二&#xff0c;信号的产生 2.1 通过按键终端产生信号 2.2 调用系统函数向进程发信号 2.3 由软条件产生信号 2.4 硬件异常产生信号 总结 前文 上文主要讲了一下进程间用管道通信的相关知识&#xff0c;本文主要带领大家深度认识一…

手机号码归属地及运营商查询

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL;public class NewMobile {public stati…

mysql手机号码规律查询

tips&#xff1a;所有手机号均为随机创建的&#xff0c;为了做查询的效果展示&#xff0c;不涉及他人隐私。 mysql手机号码规律查询 tips&#xff1a;所有手机号均为随机创建的&#xff0c;为了做查询的效果展示&#xff0c;不涉及他人隐私。数据库全号规律AAAABABAABBCCABCD(顺…

批量查询手机号码运营商信息

项目场景&#xff1a; 批量查询手机号码信息 问题描述&#xff1a; 有同事需要批量查询手机号码的运营商&#xff0c;归属地区号信息 思路&#xff1a; 百度搜索&#xff0c;爬虫取数&#xff0c;学的不精暂时还不会直接放弃python 自带一个phone库&#xff0c;缺点是查不到…

知识点速记 | 本机号码一键登录?

目录 中国移动 中国电信 很多 APP 的目前都支持「本机号码一键登录」功能。本机号码一键登录是基于运营商独有网关认证能力推出的账号认证产品。用户只需一键授权&#xff0c;即可实现以本机号码注册/登录&#xff0c;相比先前的短信验证码流程体验更优。 目前市面上有很多厂…