using System.IO;//引用 System.IO
namespace filestream
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void btnWrite_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "文本文件|*.txt|c#文件|*.cs"; if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txtPath.Text = sfd.FileName;//保存的路径
using (FileStream fs = new FileStream(txtPath.Text, FileMode.Create))
{
string txt = txtContent.Text;
byte[] buffer = Encoding.UTF8.GetBytes(txt);
fs.Write(buffer, , buffer.Length);
}
}
} private void btnRead_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "文本文件|*.txt";
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txtPath.Text = ofd.FileName;
using (FileStream fs = new FileStream(txtPath.Text, FileMode.Open))
{
//byte[] buffer = new byte[fs.Length];
//fs.Read(buffer, 0, buffer.Length); //string msg = Encoding.UTF8.GetString(buffer);
//txtContent.Text = msg; using (StreamReader sr = new StreamReader(fs,Encoding.UTF8))
{
string msg = sr.ReadToEnd();
txtContent.Text = msg;
}
}
}
}
}
}

Filestream读取或写入文件的更多相关文章

  1. c#通过FileStream读取、写入文件

    网上找过一些FileStream读取写入文件的代码,但是都有些小问题. 于是自己整理一下,以备不时之需.说明一下,以下代码我都运行过. 1.FileStream读取文件 // FileStream读取 ...

  2. php中读取以及写入文件的方法总结

    ==>读取文件内容(方法一) $fileData = fread($fileStream,filesize($filePath)); 注意: 文本文件读取到网页上显示时,由于换行符不被解释,文本 ...

  3. 读取和写入 文件 (NSFIleManger 与 NSFileHandle)

    读取和写入 文件 //传递文件路径方法 -(id)initPath:(NSString *)srcPath targetPath:(NSString *)targetPath { self = [su ...

  4. C# Byte[]数组读取和写入文件

    这个项目我用的是asp.net构建的,代码如下 protected void ByteToString_Click(object sender, EventArgs e) { string conte ...

  5. C#读取和写入文件

    一.读取文件 如果你要读取的文件内容不是很多, 可以使用 File.ReadAllText(FilePath) 或指定编码方式 File.ReadAllText(FilePath, Encoding) ...

  6. 【转】MFC中用CFile读取和写入文件2

    原文网址:http://blog.sina.com.cn/s/blog_623a7fa40100hh1u.html CFile提供了一些常用的操作函数,如表1-2所示. 表1-2  CFile操作函数 ...

  7. 在线程中进行读取并写入文件和wenjia

    新人求(胸)罩!!! import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException ...

  8. Redis 中文入库成功,读取数据写入文件乱码问题

    近期须要用到redis ,可是在编码这个问题上,纠结了非常久.        需求 :每天一个进程将中文文件入库到redis中(不定时更新) ,另外几个进程读取redis中的信息 ,并处理数据结果.使 ...

  9. Python读取和写入文件

    1 从文件中读取数据 1.1 读取整个文件 创建名为test的txt文本文件,添加内容如下所示: 123456789023456789013456789012 实现代码: with open('tes ...

随机推荐

  1. [ActionScript3.0] 逻辑或"||=" ,等于"=="和全等于"==="

    function a(o:Object):void { o||=new Object();  trace(o); } //此上下两个方法作用是一样的 function b(o:Object):void ...

  2. spring错误:<context:property-placeholder>:Could not resolve placeholder XXX in string value XXX

    spring同时集成redis和mongodb时遇到多个资源文件加载的问题 这两天平台中集成redis和mongodb遇到一个问题 单独集成redis和单独集成mongodb时都可以正常启动程序,但是 ...

  3. java内部类的继承

    1.public class OuterInnerClass extends ClassA.ClassB{        public OuterInnerClass(ClassA a)       ...

  4. 查看用户的SQL执行历史

    程序开发少不来SQL,基本都是基于SQL开发,程序仅仅起一个流程控制的作用.但是数据库本身存在许多内置的视图或者内置的表,如果打算研究SQL执行的效率已经SQL执行的历史记录,通过这些视图可以知道. ...

  5. EF连接mysql数据库生成实体模型

    声明:本人也是第一次用EF连接mysql生成实体模型 经过试验: mysql-connector-net-6.6.6 可以支持VS2012 mysql-connector-net-6.3.9 可以支持 ...

  6. Fegla and the Bed Bugs 二分

    Fegla and the Bed Bugs Fegla, also known as mmaw, is coaching a lot of teams. All these teams train ...

  7. linux启动init流程(转)

    当系统启动时,首先启动内核,内核调用init来完成引导进程.init启动时,它会在/etc/inittab内查找默认的运行级别:如id:2:initdefault:运行/etc/rc.d/init.d ...

  8. Ceph–s ceph 集群状态

    [root@ceph-mon1 ~]# ceph -s cluster 03f3afd4-4cc6-4083-a34c-845446a59cd4 health HEALTH_OK monmap e1: ...

  9. 慕课网-安卓工程师初养成-2-6 Java中的数据类型

    来源:http://www.imooc.com/code/1230 通常情况下,为了方便物品的存储,我们会规定每个盒子可以存放的物品种类,就好比在“放臭袜子的盒子”里我们是不会放“面包”的!同理,变量 ...

  10. ASPXGridView用法

    一.ASPXGridView外观显示 属性: Caption----列的标题( KeyFieldName----数据库字段 SEOFriendly 是否启用搜索引擎优化 Summary 指定分页汇总信 ...