FileStream 的文件读取与写入 之一(读写文件)
2013-05-24 02:03
197 查看
序言:FileStream 去读取文件和写文件比File有很大的优势,其中最重要的是当读取一个大文件时FileStream可以多次去读,而File就很难做到:
下面分别是写入问价和读取文件的方法:
========写入方法=======
// 首先选择一个要写入的文件
private void ChoseFilePath()
{
// 选择文件框对象
OpenFileDialog ofd = new OpenFileDialog();
// 设置默认文件路径
ofd.InitialDirectory = @"C:\Users\HP\Desktop";
// 如果用户选择了确定
if (ofd.ShowDialog() == DialogResult.OK)
{
txtFileName.Text = ofd.FileName;
}
}
// 写入到选择的文件中
private void SaveFile()
{
// 获取要保存的文件内容
string strContent = txtContent.Text.Trim();
// 创建文件流(文件路径、文件操作)
using (FileStream fs = new FileStream(txtFileName.Text, FileMode.Create))
{
//// 设置默认存储四兆大小的文件
//byte[] byteFile = new byte[1024*1024*4];
// 把文件(字符串)转换成字节数组(也有把字节转换成文件的方法)
byte[] byteFile = Encoding.UTF8.GetBytes(strContent);
// 往文件里写数据
fs.Write(byteFile, 0, byteFile.Length);
MessageBox.Show("保存成功");
}
}
========读取文件============
// 首先选择要读取的文件路径
private void ChoseReadFile()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = @"C:\Users\HP\Desktop";
if (ofd.ShowDialog() == DialogResult.OK)
{
txtReadFileName.Text = ofd.FileName;
}
}
// 读取文件
private void Read()
{
using(FileStream fs = new FileStream(txtReadFileName.Text,FileMode.Open))
{
byte[] byteData = new byte[1024*1024*4];
int length = fs.Read(byteData, 0, byteData.Length);
if (length > 0)
{
string strContent = Encoding.UTF8.GetString(byteData);
txtReadContent.Text = strContent;
}
}
}
注意:以上是简单的读取文件和写入文件的方法,并且指定了文件读取的大小。但是当读取的文件大时,肯定会报错的,后续我会总结一种循环读取大文件的方法。
下面分别是写入问价和读取文件的方法:
========写入方法=======
// 首先选择一个要写入的文件
private void ChoseFilePath()
{
// 选择文件框对象
OpenFileDialog ofd = new OpenFileDialog();
// 设置默认文件路径
ofd.InitialDirectory = @"C:\Users\HP\Desktop";
// 如果用户选择了确定
if (ofd.ShowDialog() == DialogResult.OK)
{
txtFileName.Text = ofd.FileName;
}
}
// 写入到选择的文件中
private void SaveFile()
{
// 获取要保存的文件内容
string strContent = txtContent.Text.Trim();
// 创建文件流(文件路径、文件操作)
using (FileStream fs = new FileStream(txtFileName.Text, FileMode.Create))
{
//// 设置默认存储四兆大小的文件
//byte[] byteFile = new byte[1024*1024*4];
// 把文件(字符串)转换成字节数组(也有把字节转换成文件的方法)
byte[] byteFile = Encoding.UTF8.GetBytes(strContent);
// 往文件里写数据
fs.Write(byteFile, 0, byteFile.Length);
MessageBox.Show("保存成功");
}
}
========读取文件============
// 首先选择要读取的文件路径
private void ChoseReadFile()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = @"C:\Users\HP\Desktop";
if (ofd.ShowDialog() == DialogResult.OK)
{
txtReadFileName.Text = ofd.FileName;
}
}
// 读取文件
private void Read()
{
using(FileStream fs = new FileStream(txtReadFileName.Text,FileMode.Open))
{
byte[] byteData = new byte[1024*1024*4];
int length = fs.Read(byteData, 0, byteData.Length);
if (length > 0)
{
string strContent = Encoding.UTF8.GetString(byteData);
txtReadContent.Text = strContent;
}
}
}
注意:以上是简单的读取文件和写入文件的方法,并且指定了文件读取的大小。但是当读取的文件大时,肯定会报错的,后续我会总结一种循环读取大文件的方法。
相关文章推荐
- 【python coding 2:文件读写】读取本地文件里的ip地址,循环写入到新的文件中
- android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()
- Filestream读取或写入文件
- Unicode(UTF16)文件读写终极方案---用C++标准的字节流读取,转化为宽字符(wchar_t),再以字节流写入
- python读写文件数据(换行写入以及按行读取)
- python读写Excel文件--使用xlrd模块读取,xlwt模块写入
- android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()
- [C++]读写txt文件和从txt文件中读取数据写入数组中
- 文件读写——以字符方式读取与写入文件
- 文件部分-----(3)ASCII码文件读写操作----将学生信息写入到文件中,再从文件中读取信息
- 利用POI对MS Excel进行读写(可以设置写入文件的样式),利用tm-extractor对Word进行读取操作并将信息用(log4j)日志信息显示出
- java读写properties文件,解决系统找不到指定路径,解决写入后读取正常,但文件数据未更新问题
- VC++套接字、数据库、文件读写综合应用-客户端读取文件套接字接收服务端写入数据库
- java:带有缓冲区的读写拷贝BufferedInputStream,BufferedOutputStream 读取写入文件
- 文件操作ofstream,open,close,ifstream,fin,依照行来读取数据, fstream,iosin iosout,fio.seekg(),文件写入和文件读写,文件拷贝和文件
- C语言文件的读写操作,每次读取1K,然后写入另外一个文件
- 【Android】android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()
- FileStream 的文件读取与写入 之二(简单文件加密)
- java中经常用到的读写配置文件的信息properties属性的写入,读取例子
- STM32F103 系列芯片读写AT45DB161芯片的程序和写入/读取字库文件的方法