1 / 29

第十二章 檔案系統

第十二章 檔案系統. 學習目標. 檔案系統 FileSystemInfo 磁碟目錄服務 檔案服務 資料串流 Stream FileStream 類別 StreamReader 類別 StreamWriter 類別. 檔案系統. PG. 12-3~4. 檔案系統. PATH 類別,提供檔案/目錄字串的處理。例如,從字串中取得目錄名稱、取得檔案名稱,此類別純粹針對字串處理,並不實際檢查檔案/目錄的狀況。. PG. 12-3~4. 檔案系統.

yamin
Download Presentation

第十二章 檔案系統

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第十二章 檔案系統

  2. 學習目標 • 檔案系統FileSystemInfo • 磁碟目錄服務 • 檔案服務 • 資料串流 Stream • FileStream類別 • StreamReader類別 • StreamWriter類別

  3. 檔案系統 PG. 12-3~4

  4. 檔案系統 PATH類別,提供檔案/目錄字串的處理。例如,從字串中取得目錄名稱、取得檔案名稱,此類別純粹針對字串處理,並不實際檢查檔案/目錄的狀況。 PG. 12-3~4

  5. 檔案系統 資料串流(stream):是一個在System.IO名稱空間下有個Stream抽象類別、提供位元組(byte)的讀/寫功能,像是檔案資料流(FileStream)、網路資料流(NetworkStream)、紀憶體資料流(MemoryStream)等,皆由此類別延而來 PG. 12-3~4

  6. 檔案系統 資料串流的讀取:讀取資料串流中的資料並轉換格式,例如轉為位元組陳列(byte array) 資料串流的寫入:將資料轉換格式並寫入資料串流 資料串流的搜尋(Seek):查詣移動目前指標位置。 FileStream類別:提供檔案資料流的讀寫 PG. 12-3~4

  7. 檔案系統 資料串流是否支援讀取、寫入、搜尋等功能得要看資料來源。如,networkStream就不支援搜尋功能。可以透過CanRead、CanWrite、CanSeek等屬性來判別資料串流是否支援讀/寫/搜尋等功能。 PG. 12-3~4

  8. 檔案系統 Directory服務提供的類別包含:Directory、DirectoryInfo。前者是靜態方法,後者是物件實體方法,提供:建立刪除搬移目錄、以及取得子目錄等功能。 PG. 12-3~4

  9. 檔案系統 File服務提供:File、FileInfo類別。這兩組類別的功能幾乎一模一樣。提供:建立、複製、刪除、搬移檔案等功能的靜態方法。但FileInfo類別必須建立物件實體才可以使用。 PG. 12-3~4

  10. FileSystemInfo • 提供檔案/目錄系統存取的基本功能 • Attributes • 設定或取得目前檔案的屬性 • Directory、Hidden 、ReadOnly • FullName • CreateionTime • Exists PG. 12-4

  11. DirectoryInfo • 提供磁碟目錄的建立及搬移等存取功能 • GetDirectories • GetFiles • CreateSubdirectory • Delete • MoveTo PG. 12-7

  12. 範例12-1 目錄清單 • GetDirectories subDirectories = thisDir.GetDirectories Dim subDir As DirectoryInfo For Each subDir In subDirectories txtList.Text &= subDir.Name.PadRight(30) & _ subDir.CreationTime.ToString("d").PadLeft(20) & _ vbCrLf Next PG. 12-8

  13. 範例12-2 檔案清單 • GetFiles fileList = thisDir.GetFiles("*.exe") For Each aFile In fileList With aFile txtList.Text &= _ .Name.PadRight(20).Substring(0, 20) & _ .LastAccessTime.ToShortDateString.PadLeft(20) & _ .Length.ToString("g").PadLeft(10) & " " & _ .Attributes.ToString() & vbCrLf End With Next PG. 12-11

  14. 範例12-3 建立子目錄 • CreateSubdirectory If thisDir.Exists Then thisDir.CreateSubdirectory(NewDir) CreateSubDir = True Else CreateSubDir = False End If PG. 12-13

  15. Directory • GetCurrentDirectory • 目前應用程式所在目錄 • GetLogicalDrivers • 取得邏輯磁碟代號集合 • SetCurrentDirectory • 設定應用程式目前的工作目錄 Lab12-4 ... 15 Min PG. 12-15

  16. Path類別 • GetTempPath • GetTempFilename Dim tmpFile As String = Path.GetTempFilename() PG. 12-17~19

  17. 檔案服務 • Create() • CopyTo() • MoveTo() • FileMode常數 • Append、Create、Open、... • FileAccess常數 • Read、ReadWrite、Write • FileShare常數 • None、Read、ReadWrite、Write Dim myDir As New FileInfo(path) Fs.CopyTo(desFile,true) PG. 12-20~22

  18. 範例12-4建立檔案 Dim newFile As New FileInfo(txtSource.Text) Dim fs As FileStream = newFile.Create() With newFile txtInfo.Text = "建立: " & .FullName & vbCrLf & _ "時間: " & .CreationTime.ToString & vbCrLf & _ "檔案長度: " & .Length & vbCrLf End With fs.Close() PG. 12-22~23

  19. 範例12-5複製檔案 srcFile.CopyTo(txtDesc.Text, True) PG. 12-24~25

  20. 範例12-7搬移檔案 srcFile.MoveTo(txtDesc.Text) PG. 12-26~27

  21. 範例12-7刪除檔案 delFile.Delete() PG. 12-27~28

  22. 資料串流 • 提供字元、位元組、二進位(Binary)等資料格式串列的讀寫功能 PG. 12-27~28

  23. 文字讀寫 • TextReader類別 • 提供文字格式資料的讀取 • TextWriter類別 • 提供文字格式資料的寫入 PG. 12-29~30

  24. FileStream類別(1/3) • 提供檔案系統標準的輸入/輸出 • 資料緩衝以提供較佳的存取效能 • 語法: • Path:開啟檔案的目錄位置及檔案名稱 • Mode:開啟或建立檔案模式 • Access:決定檔案唯讀或唯寫或可讀寫 • Share:決定檔案其他FileStream是否也可以開啟相同檔案 Dim myfs As New FileStream(path,mode,access,share) PG. 12-30

  25. 屬性 CanRead CanWrite Length Postion Method Write Seek Close FileStream類別(2/2) PG. 12-31

  26. StreamReader • 字元資料串流的讀取 • 提供字元編碼處理 • 語法: • Stream:任何繼承自Stream的類別 • Encoding:字元編碼格式 • Peek方法 • ReadLine方法 StreamReader(stream,encoding) PG. 12-32

  27. StreamWriter • 提供字元資料流的寫入 • 提供字元編碼格式的處理 • 語法: • Write 方法 • WriteLine方法 • Close方法 StreamWriter(stream,encoding) PG. 12-33~34

  28. 範例12-8記事本 PG. 12-35

  29. 課後回顧 • 檔案系統FileSystemInfo • 磁碟目錄服務 • 檔案服務 • 資料串流 Stream • FileStream類別 • StreamReader類別 • StreamWriter類別

More Related