国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C#Byte[]數組讀取和寫入文件

2019-11-14 13:57:49
字體:
來源:轉載
供稿:網友

這個項目我用的是asp.net構建的,代碼如下

 1  PRotected void ByteToString_Click(object sender, EventArgs e) 2         { 3  4  5             string content = this.txtContent.Text.ToString(); 6  7             if (string.IsNullOrEmpty(content)) 8             { 9                 return;10             }11 12             //string 轉為byte數組13             byte[] array = Encoding.UTF8.GetBytes(content);14 15             //將byte數組轉為string16             string result = Encoding.UTF8.GetString(array);17 18 19             Response.Write(result);20 21 22         }23         //利用byte[]數組寫入文件24         protected void writerFile_Click(object sender, EventArgs e)25         {26 27             string content = this.txtContent.Text.ToString();28 29             if (string.IsNullOrEmpty(content))30             {31                 return;32             }33 34             //將string轉為byte數組35             byte[] array = Encoding.UTF8.GetBytes(content);36 37             string path = Server.MapPath("/test.txt");38             //創建一個文件流39             FileStream fs = new FileStream(path, FileMode.Create);40 41             //將byte數組寫入文件中42             fs.Write(array, 0, array.Length);43             //所有流類型都要關閉流,否則會出現內存泄露問題44             fs.Close();45 46             Response.Write("保存文件成功");47 48 49         }50         //利用byte[]數組讀取文件51         protected void readFile_Click(object sender, EventArgs e)52         {53             string path = Server.MapPath("/test.txt");54 55             FileStream fs = new FileStream(path, FileMode.Open);56 57             //獲取文件大小58             long size = fs.Length;59 60             byte[] array = new byte[size];61 62             //將文件讀到byte數組中63             fs.Read(array, 0, array.Length);64 65             fs.Close();66 67             //將byte數組轉為string68             string result = Encoding.UTF8.GetString(array);69 70 71             Response.Write(result);72 73             74 75         }

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 荣昌县| 六安市| 清远市| 牟定县| 武山县| 新闻| 大同县| 普宁市| 克拉玛依市| 忻州市| 阜康市| 石城县| 肥乡县| 丹江口市| 甘孜| 富顺县| 定边县| 鹿泉市| 临城县| 克东县| 格尔木市| 都安| 平南县| 贞丰县| 塘沽区| 六安市| 内乡县| 汝城县| 南充市| 手游| 巩留县| 横山县| 西乡县| 当阳市| 赤峰市| 饶阳县| 酉阳| 南木林县| 铁岭市| 吉安市| 长葛市|