zoukankan      html  css  js  c++  java
  • 数据流的所有类 Winform界面 (技术改变世界cnblog)

    废话不多说,前面2篇 写过 文件与文件夹的类,今天写流,在学习过程分析出来。

    流在.net里是由Steam类表示的,该类是抽象类,无法实例化。
    处理文件输入输出的流:
    1.FileSteam
    2.BufferedStream
    3.CryptoStream
    4.MemoryStream
    5.NetworkStream


    使用文件流读写文件 FileStream,可以读写任何文件,默认是同步
    读写文本文件  使用StreamReader类与StreamWriter类(用于处理文本文件)

    代码如下:

    View Code
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;

    namespace Sream_For_All
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }
    //初始化的东西
    private void Form1_Load(object sender, EventArgs e)
    {
    txtPath1.ReadOnly = true;
    txtPath2.ReadOnly = true;
    richTextBox1.Multiline = true;
    richTextBox2.Multiline = true;
    btnWrite1.Enabled = false;
    btnWrite2.Enabled = false;
    }
    private void txtDest_TextChanged(object sender, EventArgs e)
    {
    btnWrite1.Enabled = true;
    }
    private void txtDest2_TextChanged(object sender, EventArgs e)
    {
    btnWrite2.Enabled = true;
    }

    //FileStream模块

    private void btnScan_Click(object sender, EventArgs e)
    {
    richTextBox1.Clear();
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
    txtPath1.Text = openFileDialog1.FileName;//获取文件名包含路径
    FileInfo file = new FileInfo(txtPath1.Text);
    FileStream filestream = file.OpenRead();
    byte[] buffer = new byte[1024];
    int readSize = filestream.Read(buffer, 0, 1024);
    long i = filestream.Position;
    string temp = Encoding.Default.GetString(buffer);
    richTextBox1.Text = "读取的字节数:" + readSize.ToString() + "\n";
    richTextBox1.AppendText("当前流的位置:" + i + "\n");
    richTextBox1.AppendText(string.Format("支持读取:{0}\n支持查找:{0}\n支持写入:{0}", filestream.CanRead, filestream.CanSeek, filestream.CanWrite));
    richTextBox1.AppendText("读取的内容:" + temp + "\n");
    filestream.Close();
    }
    }

    private void btnWrite_Click(object sender, EventArgs e)
    {
    richTextBox1.Clear();
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
    //源 流

    txtPath1.Text = openFileDialog1.FileName;//源文件
    FileInfo file = new FileInfo(txtPath1.Text);
    FileStream fsRead = file.OpenRead();
    //目标流
    FileStream fsWrite = File.Open(txtDest1.Text, FileMode.Create);
    int WriteSize = 1024;
    byte[] buffer = new byte[WriteSize];//定义缓冲区
    //读取
    int ReadSize = fsRead.Read(buffer, 0, WriteSize);//读了多少字节
    fsWrite.Write(buffer, 0, ReadSize);
    fsRead.Close();
    fsWrite.Flush();
    fsWrite.Close();
    richTextBox1.Text = "开始读取....\n";
    richTextBox1.AppendText("内容传递成功!");

    }
    }
    //StreamRW 模块
    private void btnRead_Click(object sender, EventArgs e)
    {
    richTextBox2.Clear();
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
    txtPath2.Text = openFileDialog1.FileName;//显示文件及路径
    using (StreamReader strRead = new StreamReader(txtPath2.Text, Encoding.Default))//这样可以不写close();
    {

    string temp = strRead.ReadToEnd();
    richTextBox2.Text = "开始读取...\n";
    richTextBox2.AppendText(temp);
    }



    }
    }

    private void btnWrite2_Click(object sender, EventArgs e)
    {
    richTextBox2.Clear();

    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
    txtPath2.Text = openFileDialog1.FileName;//源文件及路径
    using (StreamReader strRead = new StreamReader(txtPath2.Text, Encoding.Default))//这样可以不写close();
    {

    string temp = strRead.ReadToEnd();
    using (StreamWriter strWrtie = new StreamWriter(txtDest2.Text))
    {
    strWrtie.Write(temp);
    }


    richTextBox2.Text = "开始读取...\n";
    richTextBox2.AppendText("写入成功!");
    }

    }
    }
    //2进制 模块

    private void btnWrite3_Click(object sender, EventArgs e)
    {
    if (richTextBox3.Text == string.Empty)
    {
    MessageBox.Show("请输入你要写入的内容!");
    return;
    }
    else
    {
    saveFileDialog1.Filter = "二进制(*.bin)|*.bin";
    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
    {
    using (FileStream filestream = new FileStream(saveFileDialog1.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
    {

    using (BinaryWriter binWrite = new BinaryWriter(filestream))
    {
    binWrite.Write(richTextBox3.Text);
    richTextBox3.AppendText("写入成功!");

    }

    }
    }
    }
    }

    private void btnRead3_Click(object sender, EventArgs e)
    {
    richTextBox3.Clear();
    openFileDialog1.Filter = "二进制(*.bin|*.bin|All(*.*)|*.*)";
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
    using (FileStream filestream=new FileStream(openFileDialog1.FileName,FileMode.Open,FileAccess.Read))
    {
    using(BinaryReader binRead=new BinaryReader(filestream))
    {
    if (binRead.PeekChar() != -1)
    {
    richTextBox3.Text = binRead.ReadString();
    }
    }
    }
    }


    }

    }
    }



  • 相关阅读:
    定义字符串数组
    ifconfig 修改IP
    空指针与野指针的区别
    GDB和Core Dump使用笔记
    雅虎(ycsb)测试hbase(压测)
    decode函数的几种用法
    NVL函数:空值转换函数
    hive行转列,列转行
    case when then else end用法
    hive中一般取top n时,row_number(),rank,dense_ran()常用三个函数
  • 原文地址:https://www.cnblogs.com/IAmBetter/p/2324787.html
Copyright © 2011-2022 走看看