C File 檔案操作 IO流 使用例項

2021-10-02 10:23:03 字數 4684 閱讀 3167

c#檔案操作:

檔案操作的標頭檔案為system .io

屬性或方法

作用datetime getcreationtime(string path)

返回指定檔案或目錄的建立日期和時間

datetime getlastaccesstime(string path) 

返回上次訪問指定檔案或目錄的日期和時間

datetime getlastwritetime(string path)

返回上次寫入指定檔案或目錄的日期和時間

void setcreationtime(string path, datetime creationtime)

設定建立該檔案的日期和時間

void setlastaccesstime(string path, datetime lastaccesstime)

設定上次訪問指定檔案的日期和時間

void setlastwritetime(string path, datetime lastwritetime)

設定上次寫入指定檔案的日期和時間

檔案流stream使用:

流可以理解為記憶體中位元組序列,stream是所有流的抽象基類,每個具體的儲存實體都可以 通過stream派生類來實現。如filestream就是這種儲存實體

三個基本操作

1、對流進行讀取:將流中的資料讀取到具體的資料結構中

2、對流進行寫入:吧資料結構中的資料寫入到流中

3、對流進行查詢:對流內的當前位置進行查詢和修改

使用流處理檔案

流是乙個用於傳輸資料的物件,資料可以向兩個方向傳輸:

1)如果資料從外部源傳輸到程式中,這就是讀取流

2)如果資料從程式傳輸到外部源中,這就是寫入流

外部源往往是乙個檔案,但也不完全是檔案,他還可能是:

1)使用一些網路協議讀寫網路上的資料,其目的是選擇資料,或從另乙個計算機上傳送資料

2)讀寫到命名管道上

3)把資料讀寫到乙個記憶體區域上

filestream例項用於讀寫檔案中的資料,要構造filestream例項,需要以下4條資訊:

1)要訪問的檔案

2)表示如何開啟檔案的模式

3)表示檔案訪問的方式—唯讀、只寫還是讀寫

4)共享訪問—表示是否獨佔訪問檔案

filestream常用的屬性和方法:

屬性:canread 判斷當前流是否支援讀取,返回bool值,true表示可以讀取

canwrite 判斷當前流是否支援寫入,返回bool值,true表示可以寫入

方法:read() 從流中讀取資料,返回位元組陣列

write() 將位元組塊(位元組陣列)寫入該流

seek() 設定檔案讀取或寫入的起始位置

flush() 清除該流緩衝區,使得所有緩衝的資料都被寫入到檔案中

filemode.create 指示作業系統應建立新檔案,如果檔案已經存在,它將被覆蓋

filemode.createnew 指示作業系統應建立新檔案,如果檔案已經存在,將引發異常

filemode.open 指示作業系統應開啟現有檔案,開啟的能力取決於fileaccess所指定的值

filemode.openorcreate 指示作業系統應開啟檔案,如果檔案不存在則建立新檔案

filemode.truncate 指示作業系統應開啟現有檔案,並且清空檔案內容

檔案共享方式:(fileshare)

fileshare方式是為了避免幾個程式同時訪問同乙個檔案會造成異常的情況。

檔案共享方式包括四個:

fileshare.none 謝絕共享當前檔案

fileshare.read 充許別的程式讀取當前檔案

fileshare.write 充許別的程式寫當前檔案

fileshare.readwrite 充許別的程式讀寫當前檔案

使用filestream類建立檔案流物件:

filestream(string 檔案路徑,filemode 檔案開啟模式)

filestream(string 檔案路徑,filemode 檔案開啟模式,fileaccess 檔案訪問方式)

filestream(string 檔案路徑,filemode 檔案開啟模式,fileaccess 檔案訪問方式,fileshare 檔案共享方式)

例://在c盤建立a.txt檔案,使用fs流物件對檔案進行操作,fs的工作模式是新建(filemode.create)

filestream fs=new filestream(@「c:a.txt」,filemode.create);

//在c盤建立a.txt檔案,使用fs流物件對檔案進行操作,fs工作模式是新建(filemode.create)檔案的訪問模式是寫入(fileaccess.write)

filestream fs=new filestream(@「c:a.txt」,filemode.create,fileaccess.write);

//在c盤建立a.txt檔案,使用fs流物件對檔案進行操作,fs工作模式是新建(filemode.create)檔案的訪問模式是寫入(fileaccess.write)檔案的共享模式是謝絕共享(fileshare.none)

filestream fs=new filestream(@「c:a.txt」,filemode.create,fileaccess.write,fileshare.none);

使用file類來建立檔案流物件:(常用)

自定義開啟檔案的方式:file.open(string,filemode);

開啟檔案進行讀取: file.openread(string);

開啟檔案進行寫入: file.openwrite(string);

對檔案的讀寫操多不管**有多少,無非就是下面的三步:

1.建立檔案讀寫流物件

2.對檔案進行讀寫

3.關閉檔案流

使用例項(摘自—家庭財務管理 原始碼):

/// 

/// 獲取資料

///

private

void

getdata()

}}this

.d**sz.datasource = ulist;

this

.d**sz.selectionmode = datagridviewselectionmode.fullrowselect;

//整行選擇

this

.d**sz.multiselect =

false

;//不允許多行選

}//------

//刪除

private

void

toolstripbutton2_click

(object sender,

eventargs e)

int index =

this

.d**sz.selectedrows[0]

.index;

//獲取選擇行的索引

list ulist =

this

.d**sz.

datasource

as list

; ulist.

removeat

(index)

;//先清空,再重新寫入

filestream fs =

newfilestream

("shouzhi.txt"

, filemode.truncate, fileaccess.write)

;//建立檔案流

streamwriter zs =

newstreamwriter

(fs, encoding.utf8)

;foreach

(shouzhi item in ulist)

zs.close()

; fs.

close()

;getdata()

;}//----------

//修改

private

void

toolstripbutton3_click

(object sender,

eventargs e)

int index =

this

.d**sz.selectedrows[0]

.index;

list ulist =

(list

)this

.d**sz.datasource;

shouzhi sz = ulist[index]

;//獲取集合實中實體物件

frmadd add

=new

frmadd()

;add

.shouzhi = sz;

dialogresult dr =

add.

showdialog()

;if(dr == dialogresult.ok)

zs.close()

; fs.

close()

;getdata()

;}}

IO流檔案操作

file f new file string pathname 完整目錄 file f new file string parent,string child 父目錄 加 子目錄 file f new file file parent,string child 父目錄 加 子 操作方法 boolea...

CFile檔案操作

各種關於檔案的操作在程式設計中是十分常見,如果能對其各種操作都瞭如指掌,就可以根據實際情況找到最佳的解決方案,從而在較短的時間內編寫出高效的 因而熟練的掌握檔案操作是十分重要的。本文將對visual c 中有關檔案操作進行全面的介紹,並對在檔案操作中經常遇到的一些疑難問題進行詳細的分析。1 檔案的查...

C File類 檔案操作

c 語言中 file 類同樣可以完成與 fileinfo 類相似的功能,但 file 類中也提供了一些不同的方法。file 類中獲取或設定檔案資訊的常用方法如下表所示。屬性或方法 作用datetime getcreationtime string path 返回指定檔案或目錄的建立日期和時間 dat...