C#操作文本文件應(yīng)用實(shí)例簡析
作者:dongxiaohui2008
C#操作文本文件應(yīng)用實(shí)例主要向你介紹了具體的C#操作文本文件的應(yīng)用實(shí)現(xiàn),C#操作文本文件需要注意什么呢?那么本文就向你介紹相關(guān)的內(nèi)容。
C#操作文本文件應(yīng)用實(shí)例:
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.IO;
- using System.Text;
- /// ﹤summary﹥C#操作文本文件應(yīng)用實(shí)例
- /// C#操作文本文件的類
- /// 程序(網(wǎng)站)所在目錄:D:\Test
- /// 操作的文本文件:D:\Test\file
- /// ﹤/summary﹥
- public partial class _Default : System.Web.UI.Page
- {
- //在讀取txt文件中的中文時(shí)出現(xiàn)亂碼,
- //解決辦法:StreamReader sr = new StreamReader(
- fileName,Encoding.GetEncoding("gb2312"));
- protected void Page_Load(object sender, EventArgs e)
- {
- #region C#讀取文本文件 (亂碼已解決)
- {
- string fileName = Server.MapPath(@"~\file") + @"\read.txt";
- StreamReader sr = new StreamReader(fileName,
- Encoding.GetEncoding("gb2312"));
- //以gb2312字符編碼格式讀取文本。
- string str;
- string result = "";
- while ((str = sr.ReadLine()) != null)//讀取每一行
- {
- result += str;
- }
- sr.Close();
- sr.Dispose();
- }
- #endregion
- #region C#寫入文本文件C#操作文本文件應(yīng)用實(shí)例
- {
- //string path = Server.MapPath(@".\file");
- //這兩句等效。
- //string path2 = Server.MapPath(@"~\file");
- //CreateText():
- //創(chuàng)建或打開一個(gè)文件用于寫入 UTF-8 編碼的文本。
- StreamWriter rw = File.CreateText(Server.MapPath(@".\file")
- + @"\write.txt");
- rw.WriteLine("你好"); //寫入三行數(shù)據(jù)。
- rw.WriteLine("hello");
- rw.WriteLine("中國");
- rw.Flush();
- rw.Close();
- rw.Dispose();
- }
- #endregion
- #region 打開文本文件以進(jìn)行讀取。(讀取中文出現(xiàn)亂碼)
- { //C#操作文本文件應(yīng)用實(shí)例
- //OpenText():打開現(xiàn)有 UTF-8 編碼文本文件以進(jìn)行讀取。
- StreamReader sr = File.OpenText(
- Server.MapPath(@".\file") + @"\open.txt");
- StringBuilder output = new StringBuilder();
- string str;
- while ((str = sr.ReadLine()) != null)
- {
- output.Append(str + "+");
- }
- string result = output.ToString();
- sr.Close();
- sr.Dispose();
- }
- #endregion
- #region C#追加文本到現(xiàn)有文件
- { //C#操作文本文件應(yīng)用實(shí)例
- //File.AppendText():
- // 創(chuàng)建一個(gè) StreamWriter,它將 UTF-8 編碼文本追加到現(xiàn)有文件。
- StreamWriter sw = File.AppendText(
- Server.MapPath(@".\file") + @"\append.txt");
- sw.WriteLine("歡迎");
- sw.WriteLine("來");
- sw.WriteLine("中國");
- sw.Flush();
- sw.Close();
- sw.Dispose();
- }
- #endregion
- #region C#拷貝文件
- {
- string from, to;
- from = Server.MapPath(@".\file") + @"\copyFrom.txt";
- to = Server.MapPath(@".\file") + @"\copyTo.txt";
- File.Copy(from, to, true);
- //true/false:是否允許改寫目標(biāo)文件。如果目標(biāo)文件不存在,會(huì)自動(dòng)創(chuàng)建。
- }
- #endregion
- #region C#刪除文件
- {
- string delFile = Server.MapPath(@".\file") + @"\delFile.txt";
- //要?jiǎng)h除的文件路徑
- File.Delete(delFile);
- }
- #endregion
- #region C#移動(dòng)文件
- {
- //string From, To;
- //From = Server.MapPath(".") + @"\MoveFrom.txt";
- //To = Server.MapPath(@".\file") + @"\MoveFromTo.txt";
- //File.Move(From, To);//移動(dòng)并可重明名
- }
- #endregion
- #region C#創(chuàng)建目錄 // Directory - DirectoryInfo
- {
- DirectoryInfo d = Directory.CreateDirectory(
- Server.MapPath(@".\file") + @"\CreateDirectory");
- //創(chuàng)建子目錄
- DirectoryInfo d1 = d.CreateSubdirectory("CreateDirectory1");
- DirectoryInfo d2 = d1.CreateSubdirectory("CreateDirectory2");
- //應(yīng)用程序的當(dāng)前工作目錄:
- //D:\Program Files\Microsoft Visual Studio 8\Common7\IDE
- string cur = Directory.GetCurrentDirectory();
- //將當(dāng)前目錄設(shè)為Server.MapPath(@".\file")
- Directory.SetCurrentDirectory(Server.MapPath(@".\file"));
- //(在當(dāng)前工作目錄)創(chuàng)建目錄
- DirectoryInfo d3 = Directory.CreateDirectory("sixAge2");
- //創(chuàng)建目錄 C#操作文本文件應(yīng)用實(shí)例
- DirectoryInfo d4 = Directory.CreateDirectory(@"sixAge2\sixAge2_1");
- //應(yīng)用程序的當(dāng)前工作目錄
- string cur1 = Directory.GetCurrentDirectory();
- }
- #endregion
- }
- }
注釋:在D盤根目錄下創(chuàng)建以Test命明名的網(wǎng)站。。。
C#操作文本文件應(yīng)用實(shí)例的基本內(nèi)容就向你介紹到這里,希望對你了解和學(xué)習(xí)C#操作文本文件有所幫助。
【編輯推薦】
責(zé)任編輯:仲衡
來源:
CSDN博客