您的位置:首页 > 其它

文件读写,改进版

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);
}
}

}
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: