.NET中的文件IO操作实例
生活随笔
收集整理的這篇文章主要介紹了
.NET中的文件IO操作实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
從TextBox控件中寫入到txt文本
Code
//從testbox中寫入到txt文本
????????protected?void?Button5_Click(object?sender,?EventArgs?e)
????????{
????????????string?text?=?txtContent.Text;
????????????if?(!string.IsNullOrEmpty(text))
????????????{
????????????????//指定文件的完整路徑
????????????????string?fileName?=?Server.MapPath("~/txt/test.txt");
????????????????//判斷該文件是否存在
????????????????if?(File.Exists(fileName))
????????????????{
????????????????????//如果存在,就先刪掉
????????????????????File.Delete(fileName);
????????????????}
????????????????else
????????????????{
????????????????????//創建一個文件操作的流
????????????????????FileStream?stream?=?new?FileStream(fileName,?FileMode.Create);
????????????????????//創建一個寫操作流
????????????????????StreamWriter?writer?=?new?StreamWriter(stream,?Encoding.UTF8);
????????????????????//進行寫操作
????????????????????writer.Write(text);
????????????????????//清空控件中的文字
????????????????????txtContent.Text?=?string.Empty;
????????????????????//關閉流,不然出現異常
????????????????????writer.Close();
????????????????????stream.Close();
????????????????}
????????????}
????????????else
????????????{
????????????????Response.Write("<script>alert(\"空的列!\")</script>");
????????????}
????????}
然后再從生成的test.txt中讀取數據,顯示到TextBox控件中(方法同理)
Code
//讀取文本到textbox中顯示
????????protected?void?Button6_Click(object?sender,?EventArgs?e)
????????{
????????????string?fileName?=?Server.MapPath("~/txt/test.txt");
????????????if?(File.Exists(fileName))
????????????{
????????????????FileStream?stream?=?new?FileStream(fileName,?FileMode.Open);
????????????????StreamReader?reader?=?new?StreamReader(stream,?Encoding.UTF8);
????????????????txtContent.Text?=?reader.ReadToEnd();
????????????????reader.Close();
????????????????stream.Close();
????????????}
????????????else
????????????{
????????????????Response.Write("<script>alert(\"沒有test.txt文件!\")</script>");
????????????}
????????}
Code
//從testbox中寫入到txt文本
????????protected?void?Button5_Click(object?sender,?EventArgs?e)
????????{
????????????string?text?=?txtContent.Text;
????????????if?(!string.IsNullOrEmpty(text))
????????????{
????????????????//指定文件的完整路徑
????????????????string?fileName?=?Server.MapPath("~/txt/test.txt");
????????????????//判斷該文件是否存在
????????????????if?(File.Exists(fileName))
????????????????{
????????????????????//如果存在,就先刪掉
????????????????????File.Delete(fileName);
????????????????}
????????????????else
????????????????{
????????????????????//創建一個文件操作的流
????????????????????FileStream?stream?=?new?FileStream(fileName,?FileMode.Create);
????????????????????//創建一個寫操作流
????????????????????StreamWriter?writer?=?new?StreamWriter(stream,?Encoding.UTF8);
????????????????????//進行寫操作
????????????????????writer.Write(text);
????????????????????//清空控件中的文字
????????????????????txtContent.Text?=?string.Empty;
????????????????????//關閉流,不然出現異常
????????????????????writer.Close();
????????????????????stream.Close();
????????????????}
????????????}
????????????else
????????????{
????????????????Response.Write("<script>alert(\"空的列!\")</script>");
????????????}
????????}
然后再從生成的test.txt中讀取數據,顯示到TextBox控件中(方法同理)
Code
//讀取文本到textbox中顯示
????????protected?void?Button6_Click(object?sender,?EventArgs?e)
????????{
????????????string?fileName?=?Server.MapPath("~/txt/test.txt");
????????????if?(File.Exists(fileName))
????????????{
????????????????FileStream?stream?=?new?FileStream(fileName,?FileMode.Open);
????????????????StreamReader?reader?=?new?StreamReader(stream,?Encoding.UTF8);
????????????????txtContent.Text?=?reader.ReadToEnd();
????????????????reader.Close();
????????????????stream.Close();
????????????}
????????????else
????????????{
????????????????Response.Write("<script>alert(\"沒有test.txt文件!\")</script>");
????????????}
????????}
轉載于:https://www.cnblogs.com/kingfly/archive/2009/09/16/1567503.html
總結
以上是生活随笔為你收集整理的.NET中的文件IO操作实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《读者》附和偶得
- 下一篇: 玩通信设备的,来这里学习