zoukankan      html  css  js  c++  java
  • 文件流的读写操作

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace C01文件流
    {
    public partial class Form1 : Form
    {

    string path = @"D: et练习资料";

    public Form1()
    {
    InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    LoadTree(path,null);
    }

    /// <summary>
    ///
    /// </summary>
    private void LoadTree(string path, TreeNode node)
    {
    //判断1下node是不是null 如果是null代表要将指定目录下的所有的子目录直接加到TreeView下
    //将指定路径下的所有的子目录
    string[] dirs = Directory.GetDirectories(path);
    //遍历
    foreach (string dir in dirs) //D:广州传智20130228.Net训练营四期20130228.Net基础提升
    {
    TreeNode node1 = new TreeNode(Path.GetFileName(dir));
    if (node == null)// 如果是null代表要将指定目录下的所有的子目录直接加到TreeView下
    {
    tvData.Nodes.Add(node1);
    }
    else//如果不是null 就将节点加载到传进来的节点下面.
    {
    node.Nodes.Add(node1);
    }
    if (Directory.GetDirectories(dir).Length > 0)//判断当前遍历到的目录下面是否还有子目录.
    {
    LoadTree(dir, node1);//如果有 递归调用将当前遍历到的目录下的子目录加载到当前节点下.

    }
    }
    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button1_Click(object sender, EventArgs e)
    {
    //1. 创建1个文件流对象. 并给这个文件流对象指定操作的文件(路径) 还有指定操作的方式
    FileStream stream = new FileStream(@"d:2.txt", FileMode.Open);
    //2. 准备1个byte数组 以供文件流对象读取数据并放到这个数组里面.
    byte[] buffer = new byte[1024 * 1024];
    //3. 调用文件流的读数据方法 将读出来的字节放入到buffer数组中.
    stream.Read(buffer, 0, buffer.Length); //100 1M
    //4. 将字节数组以指定的编码转换为字符串
    string content = Encoding.Default.GetString(buffer);
    txtContent.Text = content;
    //5.关闭文件流
    stream.Dispose();
    }

    private void button2_Click(object sender, EventArgs e)
    {
    SaveFileDialog sfd = new SaveFileDialog();
    DialogResult res = sfd.ShowDialog();
    if (res == System.Windows.Forms.DialogResult.OK) //弹出1个文件保存的框
    {
    //1. 创建文件流对象
    FileStream stream = new FileStream(sfd.FileName, FileMode.Create);
    //2. 调用文件流写入的方法.
    string content = txtContent.Text;
    //3. 将字符串以指定的编码转换为字节数组.(二进制)
    byte[] buffer = Encoding.UTF8.GetBytes(content); //1024
    //调用文件流对象的写入方法
    stream.Write(buffer, 0, buffer.Length);
    stream.Dispose();
    }
    //
    }

    private void button3_Click(object sender, EventArgs e)
    {

    //1. 准备个用于读数据的文件流
    using (FileStream streamReader = new FileStream(@"F:HD2.tp", FileMode.Open))
    {
    //2. 准备1个用于写数据的文件流
    using (FileStream streamWriter = new FileStream(@"d:21.tp", FileMode.Create))
    {
    //3. 准备1个字节数组用于保存读出来的数据
    byte[] data = new byte[1024 * 1024 * 2];
    //4. 用读的文件流将数据读出来 放到字节数组中.
    int length = 0;
    do
    {
    length = streamReader.Read(data, 0, data.Length);
    streamWriter.Write(data, 0, length);
    } while (length >= data.Length); //1000 980
    //5. 调用写的文件流将字节数组写入.

    //6. 关闭文件流

    MessageBox.Show("操作成功!");
    }
    }

    ////1. 准备个用于读数据的文件流
    //FileStream streamReader = new FileStream(@"F:HD2.tp", FileMode.Open);
    ////2. 准备1个用于写数据的文件流
    //FileStream streamWriter = new FileStream(@"d:21.tp", FileMode.Create);
    ////3. 准备1个字节数组用于保存读出来的数据
    //byte[] data = new byte[1024 * 1024 * 2];
    ////4. 用读的文件流将数据读出来 放到字节数组中.
    //int length = 0;
    //do
    //{
    // length = streamReader.Read(data, 0, data.Length);
    // streamWriter.Write(data, 0, length);
    //} while (length >= data.Length); //1000 980
    ////5. 调用写的文件流将字节数组写入.

    ////6. 关闭文件流
    //streamWriter.Dispose();
    //streamReader.Dispose();
    //MessageBox.Show("操作成功!");

    }

    private void button4_Click(object sender, EventArgs e)
    {
    //被using管理的对象 一处using块 就会自动调用这个对象的Dispose方法
    //如果类的对象要被using管理 这个对象的类必须IDisposeable接口.
    //using的本质其实就是1个try-finally 将using中的代码生成在了try中, 调用该对象的Dispose方法写在了finally中
    //所以Dispose方法无论如何都会调用.
    using (FileStream stream = new FileStream(@"d:1.txt", FileMode.Open))
    {
    byte[] buffer = new byte[1024*1024];
    stream.Read(buffer, 0, buffer.Length);
    stream.Dispose();
    }
    }


    private void TestUsing()
    {
    using (Person p = new Person())
    {
    Console.WriteLine("我在using中");
    return;

    }// (p as IDisposable).Dispose();

    Console.WriteLine("我出来了.////");
    }

    private void button5_Click(object sender, EventArgs e)
    {
    //TestUsing();
    FileStream fs = File.Open(@"d:1.txt", FileMode.Open);
    }
    }
    }

  • 相关阅读:
    Calibrating & Undistorting with OpenCV in C++ (Oh yeah)
    opencv中标定函数calibrateCamera
    3D 障碍物感知
    在ubuntu18.04一键安装opencv3.4.1的脚本
    How to run yolov5 model using TensorRT.
    花费 3 天时间整理的代码规范示例代码,你确定不进来看看吗?
    企业级自定义表单引擎解决方案(六)--工作流挂接表单
    企业级自定义表单引擎解决方案(五)--自定义表单典型业务案例
    企业级自定义表单引擎解决方案(四)--实体对象模型实现
    企业级自定义表单引擎解决方案(三)--实体对象模型设计
  • 原文地址:https://www.cnblogs.com/kexb/p/3660400.html
Copyright © 2011-2022 走看看