• C#(三十八)之StreamWriter StreamWriter使用方法及与FileStream类的区别


    StreamReader类的属性:

    CurrentEncoding:获取流使用的字符编码

    EndOfStream:指示当前位置是否在流的末尾

    StreamReader类的方法:

    Read():读取流中的下一个字符或下一组字符。

    ReadBlock():读取一个字符块。

    ReadLine():从流中读取一行字符

    ReadToEnd():从流的当前位置读取到流的末尾

    Close():关闭当前流,并释放资源

    StreamWriter类的属性:

    Ecoding:获取被写入类型的字符编码

    例:outFile = new StreamWriter (“c://abc.txt”,false,Encoding.GetEncoding(“gb2312”));

    NewLine:当前流使用“行结束符”;

    StreamWriter类的方法:

    Write():写入数据

    WriteLine():写入数据,并添加行结束符

    Close():关闭当前流,并释放资源

    StreamXXXX类与FileStream类的区别:

    1:StreamReader/StreamWriter操作的是字符数据(char),而FileStream操作的是字节数据(byte):

    2:StreamXXXX类常用于文本的打开与保存,而FileStream则用于数据的传输。

    3:FileStream是不能指定编码(因为它看到的只是文件的二进制形式,当然无所谓编码),所以如果有中文的文本的话需要转码。

    4:FileStream是一个较底层的类,只能简单地读文件到而缓冲区,而StreamXXXX类封装了一些高级的方法,如ReadLine() (按行读取)

    5:FileStream类主要使用于大文件读写,StreamXXXXX类主要用于小文件的读写。

    StreamReader

    /// 
            /// StreanReader读取
            /// 
            private void button3_Click(object sender, EventArgs e)
            {
                path = textBox1.Text;
                if (path != "" && File.Exists(path))
                {
                    try
                    {
                        reader = new StreamReader(path, Encoding.Default);
                        string str = reader.ReadToEnd();
                        MessageBox.Show(str);
                    }
                    catch (Exception qq)
                    {
                        MessageBox.Show(qq.Message);
                    }
                    finally
                    {
                        // 关闭文件流
                        reader.Close();
                        // 关闭资源
                        reader.Dispose();
                    }
                }
                else
                {
                    MessageBox.Show("请输入正确的路径");
                    return;
                }
               
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    StreamWriter

    /// 
            /// StreamWrite写入
            /// 
            private void button4_Click(object sender, EventArgs e)
            {
                path = textBox1.Text;
                try
                {
                    if (path != "" && File.Exists(path))
                    {
                        //创建文件
                        //writer = File.CreateText(path);
                        writer = new StreamWriter(path, false, Encoding.GetEncoding("gb2312"));
                        string contact = @"十年征战梦一场,功名利禄终相忘";
                        writer.Write(contact);
                        MessageBox.Show("写入成功!");
     
                    }
                    else
                    {
                        MessageBox.Show("请输入正确的路径");
                        return;
                    }
                }
                catch (Exception qq)
                {
                    MessageBox.Show(qq.Message);
                }
                finally
                {
                    writer.Close();
                    writer.Dispose();
                }
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    测试使用全部代码:

    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();
            }
            /// 
            /// 窗体加载事件
            /// 
            private void Form1_Load(object sender, EventArgs e)
            {
     
            }
            /// 
            /// 定义一个FileStream类
            /// 
            public FileStream ff = null;
            /// 
            /// 存储文件路径
            /// 
            public string path = "";
            /// 
            /// StreamReader空对象
            /// 
            public StreamReader reader = null;
            /// 
            /// StreamWriter空对象
            /// 
            public StreamWriter writer = null;
            /// 
            /// FileStream读取
            /// 
            private void button1_Click(object sender, EventArgs e)
            {
                path = textBox1.Text;
                if (path != "" && File.Exists(path))
                {
                    ff = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                }
                else
                {
                    MessageBox.Show("请输入正确的路径");
                    return;
                }
                try
                {
                    byte[] buffer = new byte[1024 * 1024 * 2];    //定义一个2M的字节数组
                    //返回本次实际读取到的有效字节数
                    int r = ff.Read(buffer, 0, buffer.Length);    //每次读取2M放到字节数组里面
                    //将字节数组中每一个元素按照指定的编码格式解码成字符串
                    string sss = Encoding.Default.GetString(buffer, 0, r);
                    MessageBox.Show(sss);
                }
                catch (Exception qq)
                {
                    MessageBox.Show(qq.Message);
                }
                finally
                {
                    // 关闭文件流
                    ff.Close();
                    // 关闭资源
                    ff.Dispose();
                }
               
            }
            /// 
            /// FileStream写入
            /// 
            private void button2_Click(object sender, EventArgs e)
            {
                path = textBox1.Text;
                try
                {
                    if (path != "")
                    {
                        ff = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                        string content = @"大漠孤烟直,长河落日圆";
                        // 将字符串读入字节数组中。
                        byte[] buffer = Encoding.Default.GetBytes(content);
                        // 将数组写入文件
                        ff.Write(buffer, 0, buffer.Length);
                        MessageBox.Show("写入成功");
                    }
                    else
                    {
                        MessageBox.Show("请输入正确的路径");
                        return;
                    }
     
                }
                catch (Exception qq)
                {
     
                    MessageBox.Show(qq.Message);
                }
                finally
                {
                    ff.Close();
                    ff.Dispose();
                }
               
            }
            /// 
            /// StreanReader读取
            /// 
            private void button3_Click(object sender, EventArgs e)
            {
                path = textBox1.Text;
                if (path != "" && File.Exists(path))
                {
                    try
                    {
                        reader = new StreamReader(path, Encoding.Default);
                        string str = reader.ReadToEnd();
                        MessageBox.Show(str);
                    }
                    catch (Exception qq)
                    {
                        MessageBox.Show(qq.Message);
                    }
                    finally
                    {
                        // 关闭文件流
                        reader.Close();
                        // 关闭资源
                        reader.Dispose();
                    }
                }
                else
                {
                    MessageBox.Show("请输入正确的路径");
                    return;
                }
               
            }
            /// 
            /// StreamWrite写入
            /// 
            private void button4_Click(object sender, EventArgs e)
            {
                path = textBox1.Text;
                try
                {
                    if (path != "" && File.Exists(path))
                    {
                        //创建文件
                        //writer = File.CreateText(path);
                        writer = new StreamWriter(path, false, Encoding.GetEncoding("gb2312"));
                        string contact = @"十年征战梦一场,功名利禄终相忘";
                        writer.Write(contact);
                        MessageBox.Show("写入成功!");
                    }
                    else
                    {
                        MessageBox.Show("请输入正确的路径");
                        return;
                    }
                }
                catch (Exception qq)
                {
                    MessageBox.Show(qq.Message);
                }
                finally
                {
                    writer.Close();
                    writer.Dispose();
                }
            }
           
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190

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

    欢迎访问个人博客
    https://guanchao.site

    欢迎访问小程序:

    在这里插入图片描述

  • 相关阅读:
    验证计划的内容
    MacOS常见问题
    RobotFrameWork自动化测试环境搭建
    非零基础自学Java (老师:韩顺平) 第8章 面向对象编程(中级部分) 8.6 面向对象编程三大特征 && 8.7 快速入门案例
    2023齐鲁工业大学考研介绍
    《向量数据库指南》——向量数据库是小题大作的方案?
    Android DEX相关,ART加载OAT文件
    重磅|博睿数据 Bonree ONE 2023秋季版焕新发布!
    49 二叉树的最近公共祖先
    打印机打印数量和碳粉监视器 2.2--PrintLimit Print Tracking
  • 原文地址:https://blog.csdn.net/qq_39708228/article/details/125895012