visual basic.net中有三種訪問文件系統的方法:第一種是使用 visual basic 運行時函數進行文件訪問 (vb傳統方式直接文件訪問);第二種是通過.net中的system.io模型訪問;第三種是通過文件系統對象模型fso訪問。
文件是存儲在某種介質上數據的集合,就其本身來講,文件只不過是磁盤上的一系列相關的數據字節。當應用程序訪問文件時,它必須假定字節是否表示字符、數據記錄、整數、字符串等。通過指定文件的訪問類型來告訴應用程序假定什么內容。
visual basic 提供三種類型的文件訪問:
1.順序,用于在連續的塊中讀取和寫入文本文件。
2.隨機,用于讀取和寫入結構為固定長度記錄的文本或二進制文件。
3.二進制,用于讀取和寫入任意結構的文件。
使用 visual basic 運行時函數進行文件訪問
vb.net保留了vb早期的直接文件訪問的方式即通過一些相關的函數直接訪問操作文件。 下表分別列出了三種直接文件訪問類型的所有可用文件訪問函數。
函數
順序
隨機
二進制
說明
fileopen 函數
x
x
x
打開輸入或輸出文件
fileclose 函數
x
x
x
關閉對用 fileopen 函數打開的文件
input 函數
x
x
從打開的順序文件中讀取數據并將數據分配給變量。
inputstring 函數
x
返回 string 值
lineinput 函數
x
從打開的順序文件中讀取一行數據并將它賦給 string 變量。
print、printline 函數
x
將格式化的顯示數據寫入順序文件。
write、writeline 函數
x
將數據寫入順序文件。
要對文件進行操作,首先要打開文件,使用fileopen函數。
示例:以 output 模式共享方式打開c盤下的readme.txt文件。
fileopen(1, "c:/readme.txt", openmode.output, openshare.shared)
當操作文件結束后,需要關閉文件防止文件內容的丟失,此外需要重新打開文件時也需要關閉文件,用fileclose函數,關閉剛才打開的文件c:/readme.txt:
fileclose(1);
當以input模式打開順序文件時,要打開的文件必須已經存在,否則打開出錯,以output或append模式打開一個不存在的文件時,fileopen先創建該文件,然后打開。
其他的一些函數:
dir 函數:返回表示匹配指定模式或文件屬性的文件名、目錄名或文件夾名的字符串或返回驅動器卷標的字符串。
eof 函數:當到達以 random 或順序 input 模式打開的文件尾時,返回 boolean 值 true。
filecopy 函數:復制文件
filedatetime 函數:返回指示創建或最后修改文件的日期和時間的 date 值。
filelen 函數:返回以字節表示的指定文件長度的 long 值。
freefile 函數:返回一個 integer 值,表示可由 fileopen 函數使用的下一個文件號。
getattr 函數:返回表示文件、目錄或文件夾的屬性的 fileattribute 值。
loc 函數:返回一個 long 值,該值指定打開文件中當前的讀/寫位置。
lof 函數:返回一個 long 值,表示用 fileopen 函數打開的文件的大小(以字節為單位)。
seek 函數:返回一個 long 值,指定用 fileopen 函數打開的文件中的當前讀/寫位置,或設置用 fileopen 函數打開的文件中的下一個讀/寫操作的位置。
setattr 函數:設置文件屬性信息。
1、順序文件的操作
從文件中讀取字符串
有三個函數可以從順序文件中讀取字符串:input,inputstring和lineinput。
input函數
從打開的順序文件中讀取數據并將數據分配給變量。
public sub input( filenumber as integer, byref value as object)
其中參數filenumber為有效文件號;value為被賦予從文件讀取的值的變量,它不能為數組或對象變量。
下面的例子是一個寫入和讀取文本example.txt的hello,world!例子,新建一個控制臺應用程序,寫入下面的代碼查看結果。
fileopen(1, "c:/example.txt", openmode.output)
write(1, "hello,")
write(1, "world!")
fileclose(1)
dim s as string
fileopen(1, "c:/example.txt", openmode.input)
do while not eof(1)
input(1, s)
console.writeline(s)
loop
fileclose(1)
inputstring函數
返回 string 值,該值包含以 input 或 binary 模式打開的文件中的字符。
inputstring ( byval filenumber as integer, byval charcount as integer ) as string
其中參數filenumber是有效文件號,charcount是指定要讀取的字符個數的有效數值表達式。
與 input 函數不同,inputstring 函數返回它讀取的所有字符,包括逗號、回車符、換行符、引號和前導空格等。對于以 binary 訪問模式打開的文件,如果試圖在 eof 返回 true 之前用inputstring 函數讀取整個文件,則會產生錯誤。在用 inputstring 讀取二進制文件時,用 lof 和 loc 函數代替 eof 函數,而在使用 eof 函數時則使用 fileget 函數。
示例:
dim s as string
fileopen(1, " c:/example.txt ", openmode.binary)
do while not eof(1)
s = inputstring(1, 9)
console.writeline(s)
loop
fileclose(1)
lineinput 函數:
從打開的順序文件中讀取一行數據并將它賦給 string 變量。
public function lineinput (byval filenumber as integer) as string
參數 filenumber 是有效文件號。
此函數從文件一次讀取一個字符,直到遇到回車符(chr(13))或回車–換行符(chr(13) + chr(10))為止。回車–換行序列被跳過而不是附加到字符字符串上。
示例:
dim s as string
fileopen(1, " c:/example.txt ", openmode.binary)
do while not eof(1)
s = lineinput(1)
console.writeline(s)
loop
fileclose(1)
把字符串寫入文件
如果要向文件中寫入數據,可以用print/printline函數,如果要寫入的數據是字符串或是數值,則可以用write/writeline函數,要寫文件,應該先將文件以output或append方式打開,然后菜可以使用print函數或write函數。
print/printline函數:將格式化的顯示數據寫入順序文件。
public sub print( byval filenumber as integer, byval paramarray output() as object)
public sub printline( byval filenumber as integer, val paramarray output() as object )
其中參數filenumber 是有效文件號,output 是要寫入文件的零個或更多個用逗號分隔的表達式。
示例:
fileopen(1, " c:/example.txt ", openmode.output)
printline(1)
printline(1,”hello,”)
print(1,spc(4),”world!”)
fileclose(1)
write/writeline和print/printline類似
2.隨機文件的操作
隨機訪問文件中的字節構成相同的一些記錄,每個記錄包含一個或多個字段,對于一個字段的記錄對應于任一標準類型,隨機訪問文件中的所有記錄都必須具有相同長度,如果實際字符串包含的字符少于將它寫入其中的字符串元素的固定長度,則 visual basic 用空白(字符代碼 32)填充記錄中的尾隨空格。如果該字符串長于字段大小,則 visual basic 截斷它。
示例:用戶定義數據類型:
structure person
public id as integer
public monthlysalary as decimal
<vbfixedstring(15)> public name as string
<vbfixedstring(2)> public sex as string
end structure
其中vbfixedstring用于定義字符串為固定長度的字符串。
在打開一個文件進行隨機操作訪問前,應該定義一個類型該類型對應該文件已包含或未包含的記錄。
打開要進行隨機訪問的文件:
fileopen 函數:fileopen(filenumber, filename, openmode.random, , , recordlength)
filenumber 和 filename 分別指定要打開的文件的編號和文件名。recordlength 以字節為單位指定每條記錄的大小。如果 recordlength 小于寫入文件的記錄的實際長度,則會生成錯誤。
示例:打開隨機訪問的文件。
dim filenum as integer ,reclength as long, aperson as person
‘計算記錄的長度
reclength=len(aperson)
‘獲得可用的文件號
filenum=freefile()
‘打開文件
fileopen(filenum,”c:/example.txt”,openmode.random,,,reclenght)
打開文件之后就可以把記錄讀入變量中,使用fileget函數。
示例:從文件中讀取第一條記錄
dim filenum as integer ,reclength as long, aperson as person
reclength=len(aperson)
filenum=freefile()
fileopen(filenum,”c:/example.txt”,openmode.random,,,reclenght)
fileget(filenum,aperson,1);
console.writeline(aperson.id)
console.writeline(aperson.name)
console.writeline(aperson.sex)
console.writeline(aperson.monthlysalary)
fileclose(filenum)
寫入記錄:通過fileput函數來替換已經存在的記錄或新增記錄
示例:寫入五條記錄到文件中:
‘自定義數據類型
structure person
public id as integer
public name as string
end structure
sub writedata()
dim myrecord as person
dim recordnumber as integer
' 隨機方式打開.
fileopen(1, "c:/example.txt", openmode.binary)
for recordnumber = 1 to 5 '
myrecord.id = recordnumber ' define id.
myrecord.name = "my name" & recordnumber '創建一個字符串
fileput(1, myrecord) ' 寫入文件
next recordnumber
fileclose(1)
end sub
以上代碼實現了隨機訪問的方式打開文件,并寫入記錄。
二進制文件的操作
打開要進行二進制訪問的文件
fileopen(filenumber, filename, openmode.binary)
關閉要進行二進制訪問的文件
fileclose(filenumber)
如果保持較小的文件大小很重要,則采用二進制訪問。因為二進制訪問不要求固定長度的字段,所以類型聲明可以省略字符串長度參數。這使得以通過生成變長記錄來節省磁盤空間。使用二進制型訪問的類型定義為:
structure person
dim id as integer
dim name as string
dim monthlysalary as decimal
dim sex as string
end structure
使用變長字段的二進制輸入/輸出的缺點是無法隨機訪問記錄,而必須按順序訪問它們才能了解每條記錄的長度。仍可直接轉到文件中的指定字節位置,但是如果字段是變長的,則不知道哪個記錄位于哪個字節處。
(部分翻譯自msdn)
新聞熱點
疑難解答
圖片精選