第三章 檔案系統操作

2021-08-29 20:02:33 字數 4643 閱讀 4934

在tango庫中,檔案和目錄儲存單位通常用filepath例項來描述。

建立乙個filepath很簡單,用char提供構造器。檔案路徑不包含ansi字元,而採用utf-8編碼。如下例:

auto path = new filepath(「name」);

建立一檔案和資料夾需要區分開,建立檔案用path.create,建立資料夾用

path.createfolder

重新命名乙個檔案會把它從乙個地方移動到另乙個地方:

path.rename(」/directory/otherfilename」);

複製乙個檔案保留它的原始時間戳:

path.copy("source.name");

刪除乙個檔案或資料夾用path.remove

列舉資料夾內容用

foreach(name;path.tolist)

//do something with the contained file or folder name

有乙個另外的低階tolist()通過委託進行更強大地控制:

path.tolist(void delegate(char parent, char name,bool isdir) dg );

這個tolist()版本會用於構建自定義檔案訪問器,並且用於#filescan模組

底層作業系統檢測到乙個錯誤就丟擲乙個io異常。如試圖移除乙個不存在的或唯讀的檔案將產生乙個異常。

(注意,檔案管道在0.99.8版中已重新命名到tango.io.device.file模組,相應的,使用fileconduit的地方改為file)

檔案訪問設計意圖是通過檔案管道(fileconduit)進行操作,提供包括流訪問和隨機訪問檔案內容的方法。

開啟乙個檔案讀取內容用如下形式:

auto conduit = new fileconduit(「myfilepath」);

開啟乙個檔案用於讀和寫操作需要明確宣告檔案型別:

auto conduit = new fileconduit(「myfilepath」,fileconduit.writecreate);

檔案管道(fileconduit)使我們可以直接訪問未知型別的檔案內容。在本例下面這個例子中,我們直接複製乙個檔案到控制台:

//開啟乙個檔案用於讀

auto from = new fileconduit(「test.txt」);

//顯示檔案內容到控制台

stdout.stream.copy(from);

現在我們複製乙個檔案到另乙個裡邊:

//開啟乙個檔案用於讀

auto from = new fileconduit(「test.txt」);

//開啟另乙個檔案用於寫

auto to = new fileconduit(「copy.txt」,fileconduit.writecreate);

//複製檔案

to.output.copy(from);

載入乙個實體檔案到記憶體,可考慮使用如下形式:

//開啟乙個檔案用於讀

auto file = new fileconduit(「test.txt」);

//建立乙個陣列容納實體檔案

auto content = new char[file.length];

//讀取檔案內容,返回值是讀取的位元組數

auto bytesread = file.input.read(content)

相反,也可以直接寫內容到乙個檔案管道,如下:

//開啟個檔案用於寫

auto to = new fileconduit(「test.txt」,fileconduit.writecreate);

//寫乙個陣列內容到該檔案

auto byteswritten = to.output.write(content);

檔案管道(fileconduit)同樣支援隨機io,在本例中,我們使用seek()重定向檔案位置,增加了一點風味,利用乙個草案(reader&writer對)進行簡單的輸入輸出型別測定:

//開啟乙個檔案用於讀和寫

auto file = new fileconduit(「random.bin」,fileconduit.readwritecreate);

//繫結乙個草案(protocol)到該管道

auto read = new reader(file);

auto write = new writer(file);

int x = 10;

char y = 「hello」;

//write data,and flush output since protocol io is buffered

write(x)(y)();

//重定向到檔案開始

file.seek(0);

//重新讀回資料

read(x)(y);

注意上面的例子中,我們使用了緩衝io(buffered io),通過草案(protocol),因而需要在重新確定當前檔案位置前沖洗輸出。

當我們不需要更長時間使用時,每個檔案管道(fileconduit)應該明確地關閉。為此目的,可以方便地使用乙個scope表示式。

auto file = new fileconduit(「myfilepath」);

scope(exit)

file.close;

大體積檔案沒有完全地完成或複製操作不完整時,io異常將被丟擲。例如,當使用乙個不可用的檔案時。

file是乙個簡單的類,用於讀寫或再次新增檔案內容,例如讀取全部檔案內容:

auto file = new file(「myfile」);

auto content = file.read();

基本的檔案在呼叫返回前被關閉。file應該避免假定檔案內容,因此,上面的例子返回乙個無型別陣列。當和文字檔案一起工作,它要強制轉換返回值去闡述正確的資料型別。文字檔案通常轉換成乙個字元陣列(char):

auto file = new file(「myfile」);

auto content = cast(char)file.read();

import text=tango.text.util;

auto file = new file(「myfile」);

auto content = cast(char)file.read();

auto lines = text.splitlines(content);

使用乙個foreach來迭代:

foreach(line;text.lines(content))

//針對每一行做某事

檔案可以設定成乙個陣列的內容:

char mytext;

file.write(mytext);

檔案內容新增用類似的方式:

以上這些每個方法屬於filepath,通過path方法被展示。因此你能獲取檔案大小,重新確定檔案位置,移除它等等。

底層作業系統或檔案系統錯誤發生時,file 丟擲乙個io異常,如嘗試寫乙個唯讀檔案。

這裡展示各種檔案系統控制,現在檔案系統(filesystem)提供取回和設定當前工作目錄和轉換乙個路徑成它的絕對路徑形式的便利。訪問當前目錄名如下操作:

auto name = filesystem.getdirectory;

改變當前目錄是類似的。

absolutepath方法接受乙個filepath例項,然後轉換它到相應的當前工作目錄的絕對路徑形式,絕對路徑形式通常開始於乙個路徑分隔符,或乙個儲存識別符號,並且不包括』.』或』..』在路徑的任何地方。如果提供的路徑已經是絕對的,就返回乙個未經改變的。

取回或設定當前目錄失敗將丟擲乙個異常。傳遞乙個無效路徑給absolutepath(絕對路徑)將產生乙個異常。

檔案系統的儲存裝置顯示在這裡,在win32平台,根(roots)描述為裝置字母,在linux平台,通過/etc/mtab/描述裝置定位。列舉檔案儲存裝置是可行的:

foreach(name;filrroots.list)

//針對每個name做某事

fileroots 異常

下層的作業系統或檔案系統錯誤發生時,將丟擲乙個io異常。

檔案掃瞄(瀏覽)(filescan)

filescan模組封裝filepath.tolist,以提供更多具體方法。主要的特徵是filescan再次提起資料夾樹(folder-trees),並且生成乙個容納檔案和資料夾的列表。產生乙個d檔案和它們所在資料夾的列表的例程如下:

import tango.io.filescan;

auto scan = new filescan;

scan(new filepath(「.」,」.d」);

foreach(folder;scan.folders)

cout(folder).newline;

foreach(file;scan.files)

cout(file).newline;

這個例子執行一次掃瞄,穿過所有結尾為「.d」的檔案,在當前目錄開始,一直擴充套件到所有子目錄,每個資料夾包含的檔案被顯示到控制台,隨後形成乙個檔名清單。

filescan有許多方法過載上面那個過分簡單化的檔案過濾例子,使用委託(delegate):

bool delegate(filepath path,bool isdir)

委託的返回值為true就新增例項,否則就排除它。引數isdir指出例項是否是乙個資料夾或檔案。

沒有明確的異常丟擲

shell 第三章 檔案系統中跳轉

the symbol refers to the working directory and the symbol refers to the working directory s parent directory.可忽略 short cut result cd到家目錄 home director...

linux 第三章 檔案操作

1 生產任意大小的檔案 root localhost dd test root localhost dd test dd if dev zero of junk.data bs 1k count 10 10 0 records in 10 0 records out 10240 bytes 10 k...

作業系統第三章概要

處理機排程 多道程式環境下,動態的把處理機分配給就緒佇列中的乙個程序使之執行。提高處理機的利用率 改善系統效能,很大程度上取決於處理機排程的效能。作業進入系統駐留在外存的後備佇列上,再至調入記憶體執行完畢,可能要經歷下述 排程 高階排程又稱作業排程或長程排程 接納排程 中級排程又稱交換排程或中程排程...