久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

Hi! 歡迎來到 [c#,,文件讀寫的操作

 gideshi 2011-12-02

1、文件的讀與寫:

 

在c#中,,讀寫文件的操作流程:

1)創(chuàng)建一個FileStream

2)創(chuàng)建閱讀器或者寫入器(針對不同情況的不同需求)

3)執(zhí)行讀或者寫的操作

4)關(guān)閉閱讀器或者寫入器

5)關(guān)閉這個FileStream

其中,,使用FileStream的時候,需要引用命名空間:

 

using System.IO;

 

 

2,、文件流(FileStream):

 

step ①:創(chuàng)建文件流:

在讀寫文件的過程中,,首先要開啟文件流;

在c#中,,使用FileStream 類創(chuàng)建文件流。

 

FileStream(String FilePath , FileMode)

 

 

在這里,,傳入了兩個參數(shù),,F(xiàn)ilePath 是對應(yīng)文件讀寫的path,

FileMode 是對要進行讀寫文件的打開方式,。

其中,,參數(shù)FileMode是枚舉(enum)類型,F(xiàn)ileMode有如下成員:

 

1)Create:新建一個指定名稱的文件(如果該文件存在,則替換舊文件)

2)CreateNew:新建一個文件,。

3)Open:打開一個文件,,使用它的時候,打開的指定文件必須存在,,否則會發(fā)生異常,。

4)OpenOrCreate:類似于成員Open,如果文件不存在,,則新創(chuàng)建一個并打開,。

 

step ②:關(guān)閉文件流:

使用完畢讀寫器,記得要關(guān)閉讀寫器,,這里用到了FileStream 對象中的.close()方法,。

 

3、文件讀寫器:

 

1)StreamWriter寫入器:

首先要創(chuàng)建好文件流,,然后再創(chuàng)建閱讀器或者寫入器(根據(jù)需求),;

在這里,寫入器就用到了StreamWriter類,,用來將數(shù)據(jù)寫入文件流中,,

打開文件流后就可以new一個StreamWriter的對象。

StreamWriter可以調(diào)用的方法如下:

 

 

StreamWriter.Write();//寫入流,。 
StreamWriter.WriteLine();//寫入一行數(shù)據(jù)后自動換行,。 
StreamWriter.Close();//關(guān)閉寫入器

2)StreamReader讀取器:

 

同理,使用前要創(chuàng)建好文件流,,然后創(chuàng)建讀取器(StreamReader),;

讀取器用到了StreamReader類,調(diào)用方法如下:

 

StreamReader.ReadLine();//讀取文件流中的一行數(shù)據(jù),,返回字符串,。 
StreamReader.ReadToEnd();//從當(dāng)前位置讀取到結(jié)束,返回字符串,。 
StreamReader.Close();//關(guān)閉讀取器,。

demo:

 

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 
 
namespace MyPerson 
{ 
    public partial class FileStreamRW : Form 
    { 
        public FileStreamRW() 
        { 
            InitializeComponent(); 
        } 
 
        private void btnRead_Click(object sender, EventArgs e) 
        { 
            string path, content; 
            path = txtPath.Text; 
            content = txtContent.Text; 
            if (String.IsNullOrEmpty(path) == true) 
            { 
                MessageBox.Show("路徑不能為空"); 
                return; 
            } 
            try 
            { 
                //創(chuàng)建文件流 
                FileStream myFs = new FileStream(path, FileMode.Open); 
                //創(chuàng)建讀取器 
                StreamReader mySr = new StreamReader(myFs); 
                //讀取文件所有的內(nèi)容 
                content = mySr.ReadToEnd(); 
                //將讀取到的內(nèi)容賦給txtCotent控件。 
                txtContent.Text = content; 
                //關(guān)閉讀取器和文件流,。 
                mySr.Close(); 
                myFs.Close(); 
            } 
            catch (Exception ex) 
            { 
                MessageBox.Show(ex.Message.ToString()); 
            } 
        } 
 
        private void btnWrite_Click(object sender, EventArgs e) 
        { 
            string path, content; 
            path = txtPath.Text; 
            content = txtContent.Text; 
            if (String.IsNullOrEmpty(path) == true) 
            { 
                MessageBox.Show("文件路徑不能為空"); 
                return; 
            } 
            try 
            { 
                //創(chuàng)建文件流 
                FileStream myFs = new FileStream(path, FileMode.Create); 
                //創(chuàng)建寫入器 
                StreamWriter mySw = new StreamWriter(myFs); 
                //將敲擊的內(nèi)容寫入文件中,。 
                mySw.Write(content); 
                MessageBox.Show("寫入成功!"); 
                //關(guān)閉寫入器和文件流,。 
                mySw.Close(); 
                myFs.Close(); 
            } 
            catch (Exception ex) 
            { 
                MessageBox.Show(ex.Message.ToString()); 
            } 
        } 
    } 
}  

 

4,、文件和目錄操作:

 

1)File類和Directory類:

①:File類的方法:

方法 說明
Exists(string path) 檢查指定的文件是否存在,返回bool值,。
Copy(string SourceFilePath , string DestinationFilePath) 將指定path的源文件中內(nèi)容復(fù)制到目標(biāo)文件夾,,如果目標(biāo)文件夾不存在,則在指定路徑中新建立一個文件。
Move(string sourceFileName , string destFileName) 將指定的文件移動到一個新的path,。
Delete(string path) 刪除指定的文件,,如果指定的文件不存在,也不會發(fā)生異常,。

 

②:Directory類的方法:

 

方法 說明
Exists(string path) 檢查指定的文件是否存在,。
Move(string sourceDirName , string destFileName) 將指定的文件移動到一個新的path。
Delete(string , bool) 刪除指定的dir,,如果為true,,則刪除子目錄中的所有內(nèi)容。

 

2)靜態(tài)類與非靜態(tài)類:

File類和Directory類在使用方法的時候不需要實例化,,直接.方法名() 就可以使用,。

靜態(tài)類和非靜態(tài)類的區(qū)別:

靜態(tài)類 非靜態(tài)類
用static 修飾 可以包含靜態(tài)成員
只包含靜態(tài)成員 可以包含靜態(tài)成員
不可以包含實例成員 可以包含實例成員
使用類名調(diào)用靜態(tài)成員 使用實例化對象調(diào)用非靜態(tài)成員
不能被實例化 可以被實例化
不能包含實例構(gòu)造函數(shù) 包含實例構(gòu)造函數(shù)

 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點,。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報,。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多