Fileatream表示文件流,它能够打开和关闭文件,并对文件进行单字节的读写操作。 StreamReader和StreamWriter以文本方式对流进行读写操作。建立一个文本文件,分别使用上面两种方
2013-05-14 20:00
851 查看
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 WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//读文件
if (string.IsNullOrEmpty(this.textBox1.Text.Trim()))
{
MessageBox.Show("必须输入待读文件路径及文件名");
return;
}
FileStream fs =
new FileStream(this.textBox1.Text,
FileMode.OpenOrCreate, FileAccess.Read);
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, bytes.Length);
this.textBox2.Text = Encoding.UTF8.GetString(bytes);
fs.Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
//写文件
if (string.IsNullOrEmpty(this.textBox1.Text.Trim()))
{
MessageBox.Show("必须输入待写文件路径及文件名");
return;
}
FileStream fs =
new FileStream(this.textBox1.Text,
FileMode.Create, FileAccess.Write);
byte[] bytes = Encoding.UTF8.GetBytes(this.textBox2.Text.Trim());
fs.Write(bytes, 0, bytes.Length);
fs.Dispose();
}
}
}
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 WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//读文件
if (string.IsNullOrEmpty(this.textBox1.Text.Trim()))
{
MessageBox.Show("必须输入待读文件路径及文件名");
return;
}
FileStream fs =
new FileStream(this.textBox1.Text,
FileMode.OpenOrCreate, FileAccess.Read);
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, bytes.Length);
this.textBox2.Text = Encoding.UTF8.GetString(bytes);
fs.Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
//写文件
if (string.IsNullOrEmpty(this.textBox1.Text.Trim()))
{
MessageBox.Show("必须输入待写文件路径及文件名");
return;
}
FileStream fs =
new FileStream(this.textBox1.Text,
FileMode.Create, FileAccess.Write);
byte[] bytes = Encoding.UTF8.GetBytes(this.textBox2.Text.Trim());
fs.Write(bytes, 0, bytes.Length);
fs.Dispose();
}
}
}
相关文章推荐
- 实验 4.2.4 实现文件复制 1. 用文件流的 I/O 函数实现一个文件拷贝程序,将一个文件拷贝到另一个文件。 2. 分别用字节读,行读,任意大小读的方式。
- 使用StreamReader与StreamWriter进行文本文件读写
- 使用基本字节输入流,字节输出流一次读写一个字节来复制文本 文件
- 一个Download.jsp文件 (采用文件流读写方式。可以防止doc.excl等文件打开时乱码问题)
- Access数据库:它已经被别的用户以独占方式打开,操作必须使用一个可更新的查询,不能锁定文件
- 分别使用(字符流)和(字节流)对文件进行读写操作
- iOS使用读写plist文件方式对数据进行增删改查
- 关于c#文件操作Streamwriter,streamreader的使用
- 文件操作之增删改查3---文件的修改,f.replace(),在linux里的一些应用sed,with语句方法来打开一个或多个文件避免忘记关闭,python一行写的太长,怎么编写多行的规范
- linux编程 fmemopen函数打开一个内存流 使用FILE指针进行读写访问
- 使用java类SequenceInputStream 对一个文件进行分割和合并的操作
- python使用codecs模块进行文件操作-读写中英文字符
- FMDB多线程读写问题,使用FMDataBaseQueue操作可以解决同时打开一个链接de读写问题
- java 如何使用缓冲区对文件进行读写操作
- C语言 文件的概念与操作(打开、读写和关闭等)
- 8IO流的分类字符流复制文本文件InputStream和outputStream的概述:分别使用字符流和字节流进行图片的复制字节流复制文本文件字节打印流 PrintStream 字符打印流 Prin
- 使用O_APPEND标志打开文件对文件进行lseek后进行读写的问题
- 文件打开关闭与读写等基本操作 C++
- 使用Octave音频处理(一):对文件进行读写操作
- 操作、压缩MDB文件;使用ADO读写Mysql数据库;建立数据库和表的VBS脚本代码(2013-08-26 11:39:34)