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. oracle修改列的类型

    alter table table_name modify column_name datatype;

  2. C++学习9 this指针详解

    this 是C++中的一个关键字,也是一个常量指针,指向当前对象(具体说是当前对象的首地址).通过 this,可以访问当前对象的成员变量和成员函数. 所谓当前对象,就是正在使用的对象,例如对于stu. ...

  3. 《一课经济学》书摘笔记I

    人在经济活动中追求私利的天性,以及天生短视的倾向(即总是只关注某项政策的即时影响,或者只关注政策对某个特殊群体产生的影响,而不去探究那项政策对所有群体造成的长远影响)以上种种致使经济规律的研究复杂艰难 ...

  4. 为什么wait(),notify()和notifyAll()必须在同步块或同步方法中调

    我们常用wait(),notify()和notifyAll()方法来进行线程间通信.线程检查一个条件后就行进入等待状态,例如,在"生产者-消费者"模型中,生产者线程发现缓冲区满了就 ...

  5. django提供xml下载

    def test_file_download(request): wb = export_to_xls() response = HttpResponse() response['Content-Ty ...

  6. 山东省第六届ACM省赛

    A.Nias and Tug-of-War(sort排序) B.Lowest Unique Price(set+map) C.Game!(博弈) D.Stars E.BIGZHUGOD and His ...

  7. WPF异步调用

    this.Dispatcher.BeginInvoke(new Action(()=> this.textBlock1.Text = DateTime.Now.ToString("HH ...

  8. docker错误

     错误:cannot enable tty mode on non tty input 错误产生: root@machine1:/data# echo test|docker exec -i 68 ...

  9. 《Code Complete》ch.22 开发者测试

    WHAT? 单元测试(Unit Testing):是将一个程序员或一个开发团队所编写的,一个完整的类.子程序或者小程序,从完整的系统中隔离出来进行测试 组件测试(Component Testing): ...

  10. jdk线程的同步问题

    一.银行取款引出的问题 模拟银行取钱的例子: public class ThreadDemo06 { public static void main(String[] args) { Bank ban ...