文件读写,改进版

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

文件读写:需要实现的功能是:点击1,弹出文件打开对话框OpenFiledialog,选择要读取的文件,点击确定之后,把文件的路径显示在1上,然后点击读取,把文件的内容显示在3上面;

同理,文件的写入,我们在3中写好内容之后,点击2,弹出文件保存对话框SaveFiledialog,然后选择好路径之后,点击写入,就将我们在3中写好的内容,保存在指定的路径了。

现在实现功能:

PRivatevoidtxtReadPath_Click(objectsender, EventArgs e)

{//打开文件对话框using(OpenFileDialog openFileDialog =newOpenFileDialog())

{if(openFileDialog.ShowDialog() ==DialogResult.OK)

{//显示文件的全路径txtReadPath.Text =openFileDialog.FileName;

}

}

}///<summary>///读取文件///</summary>///<param name="sender"></param>///<param name="e"></param>privatevoidbtnNewRead_Click(objectsender, EventArgs e)

{//创建文件流(读文件的时候,FileMode选择Open)using(FileStream fileRead =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, EventArgs e)

{//打开文件对话框using(OpenFileDialog openFileDialog =newOpenFileDialog())

{if(openFileDialog.ShowDialog() ==DialogResult.OK)

{//显示文件的全路径txtReadPath.Text =openFileDialog.FileName;

}

}

}///<summary>///读取文件///</summary>///<param name="sender"></param>///<param name="e"></param>privatevoidbtnNewRead_Click(objectsender, EventArgs e)

{//创建文件流(读文件的时候,FileMode选择Open)using(FileStream fileRead =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>///<param name="sender"></param>///<param name="e"></param>privatevoidtextWritePath_Click(objectsender, EventArgs e)

{//创建文件保存对话框using(SaveFileDialog saveFileDialog =newSaveFileDialog())

{if(saveFileDialog.ShowDialog() ==DialogResult.OK)

{//全路径textWritePath. Text =saveFileDialog.FileName;

}

}

}privatevoidbtnWrite_Click(objectsender, EventArgs e)

{//将内容写入到指定的路径文件中using(FileStream fileWrite =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);

}

}

}

}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航