文件读写,改进版
2015-11-23 22:41
232 查看
文件读写:需要实现的功能是:点击1,弹出文件打开对话框OpenFiledialog,选择要读取的文件,点击确定之后,把文件的路径显示在1上,然后点击读取,把文件的内容显示在3上面;
同理,文件的写入,我们在3中写好内容之后,点击2,弹出文件保存对话框SaveFiledialog,然后选择好路径之后,点击写入,就将我们在3中写好的内容,保存在指定的路径了。
现在实现功能:
privatevoidtxtReadPath_Click(objectsender,EventArgse) { //打开文件对话框 using(OpenFileDialogopenFileDialog=newOpenFileDialog()) { if(openFileDialog.ShowDialog()==DialogResult.OK) { //显示文件的全路径 txtReadPath.Text=openFileDialog.FileName; } } } ///<summary> ///读取文件 ///</summary> ///<paramname="sender"></param> ///<paramname="e"></param> privatevoidbtnNewRead_Click(objectsender,EventArgse) { //创建文件流(读文件的时候,FileMode选择Open) using(FileStreamfileRead=newFileStream(txtReadPath.Text,FileMode.Open)) { //创建btpe数组 byte[]fileByte=newbyte[fileRead.Length]; //把文件数据读取到数组中 fileRead.Read(fileByte,0,fileByte.Length); //现在将数据转化为string格式的数据,好显示在txt控件中,通过下面的方法把byte数组转化为string字符串 txtContent.Text=Encoding.Default.GetString(fileByte); } }
这部分就实现了读取文件的数据到文本框中,其中Encoding.Default.GetString这个方法只能,读取文本文件。不能读取Word,所谓文本文件就是可以用记事本打开的。
Encoding.Default.GetString这个方法还有个问题,会乱码:解决方法: txtContent.Text=Encoding.GetEncoding("utf-8").GetString(fileByte); 修正之后: 好了,下面开始文件的写入功能实现: 步骤:我们先在文本框中输入内容,然后点击第二个文本框,弹出保存对话框,我们选择路径,输入要保存的名字(带上后缀)。然后点击保存。 所有的代码如下:
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.IO; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; usingSystem.Windows.Forms; namespaceFileReadDemo { publicpartialclassForm1:Form { publicForm1() { InitializeComponent(); } privatevoidtxtReadPath_Click(objectsender,EventArgse) { //打开文件对话框 using(OpenFileDialogopenFileDialog=newOpenFileDialog()) { if(openFileDialog.ShowDialog()==DialogResult.OK) { //显示文件的全路径 txtReadPath.Text=openFileDialog.FileName; } } } ///<summary> ///读取文件 ///</summary> ///<paramname="sender"></param> ///<paramname="e"></param> privatevoidbtnNewRead_Click(objectsender,EventArgse) { //创建文件流(读文件的时候,FileMode选择Open) using(FileStreamfileRead=newFileStream(txtReadPath.Text,FileMode.Open)) { //创建btpe数组 byte[]fileByte=newbyte[fileRead.Length]; //把文件数据读取到数组中 fileRead.Read(fileByte,0,fileByte.Length); //现在将数据转化为string格式的数据,好显示在txt控件中,通过下面的方法把byte数组转化为string字符串 //txtContent.Text=Encoding.Default.GetString(fileByte);//这个方法会乱码、 txtContent.Text=Encoding.GetEncoding("utf-8").GetString(fileByte); } } ///<summary> ///文件写入 ///</summary> ///<paramname="sender"></param> ///<paramname="e"></param> privatevoidtextWritePath_Click(objectsender,EventArgse) { //创建文件保存对话框 using(SaveFileDialogsaveFileDialog=newSaveFileDialog()) { if(saveFileDialog.ShowDialog()==DialogResult.OK) { //全路径 textWritePath.Text=saveFileDialog.FileName; } } } privatevoidbtnWrite_Click(objectsender,EventArgse) { //将内容写入到指定的路径文件中 using(FileStreamfileWrite=newFileStream(textWritePath.Text.Trim(),FileMode.Create)) { //Write方法,需要传入Byte数组,这个时候,就有个问题了,我们在文本控件中,输入的内容是字符串的,不过同理,字串串也可以转化为byte数组 byte[]fileWriteByte=Encoding.GetEncoding("utf-8").GetBytes(txtContent.Text.Trim()); fileWrite.Write(fileWriteByte,0,fileWriteByte.Length); } } } }
相关文章推荐
- java基础String
- java动态代理(JDK和cglib)
- scala学习笔记1-泛型、界定、形变、this.type、复合类型、抽象类型
- 如何解决“The identity used to sign the executable is no longer valid”
- python学习笔记(10)
- RPM深入-系统安全方面的应用
- 事件传值及ComboBox遍历控件
- iOS开发44-iOS Swift数组
- 应用系统数据缓存设计
- ubantu安装svn服务器
- ListView的多种布局显示(三)
- 如何生成mp4文件
- Android SDK 命令开发项目
- NSDictionary基本用法
- Time的hadoop学习笔记之--搭建有三台主机的Hadoop集群
- 关于求分组中最新、最大的数据的处理。
- SQL Server索引
- Android性能优化之常见的内存泄漏
- Android分析内存
- java基础多线程2