intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Input and Output

Chia sẻ: Lavie Lavie | Ngày: | Loại File: PDF | Số trang:0

51
lượt xem
2
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Objectives of Input and Output is survey IO facilities in .NET Framework Class Library (file and directory management, text files, binary files, object serialization). It includes IO Library, Files and directories, File System Info, Utility classes, Choice of file/directory class.

Chủ đề:
Lưu

Nội dung Text: Input and Output

  1. Input and Output
  2. Objectives • Survey IO facilities in .NET Framework Class Library – file and directory management – text files – binary files – object serialization 2
  3. IO Library • Input/output library in System.IO namespace • Support provided for: – file and directory management – text files – binary files – simple type conversion to/from binary 3
  4. Files and directories • Two primary classes to work with files and directories – perform file system interaction – generally do not manipulate file contents – derived from common base FileSystemInfo DirectoryInfo FileInfo 4
  5. FileSystemInfo • Files and directories have some operations in common – provided in base class FileSystemInfo public abstract class FileSystemInfo ... { public abstract string Name { get; } name public string FullName { get; } public string Extension { get; } public abstract bool Exists { get; } public DateTime CreationTime { get; set; } characteristics public DateTime LastAccessTime { get; set; } public DateTime LastWriteTime { get; set; } public FileAttributes Attributes { get; set; } delete public void Delete() ... ... } 5
  6. DirectoryInfo • DirectoryInfo represents a directory – methods provided to examine and manipulate – inherits common operations from FileSystemInfo public sealed class DirectoryInfo : FileSystemInfo { constructor public DirectoryInfo(string path) ... public DirectoryInfo Parent { get; } navigation public DirectoryInfo Root { get; } public void Create () ... manipulation public void MoveTo (string destDirName) ... public void Delete (bool recursive ) ... public DirectoryInfo CreateSubdirectory(string path ) ... public FileInfo [] GetFiles () ... contents public DirectoryInfo [] GetDirectories () ... public FileSystemInfo[] GetFileSystemInfos() ... ... } 6
  7. FileInfo • FileInfo represents a file – methods provided to examine and manipulate – inherits common operations from FileSystemInfo public sealed class FileInfo : FileSystemInfo { constructor public FileInfo(string fileName) ... length public long Length { get; } public string DirectoryName { get; } location public DirectoryInfo Directory { get; } public FileInfo CopyTo(string destFileName) ... manipulation public FileInfo CopyTo(string destFileName, bool overwrite) ... public void MoveTo(string destFileName) ... ... } 7
  8. Application: List contents • Can examine contents of a directory void List(string path) { DirectoryInfo directory = new DirectoryInfo(path); FileInfo [] files = directory.GetFiles (); contents DirectoryInfo[] directories = directory.GetDirectories(); files foreach (FileInfo f in files) Console.WriteLine(f.Name); directories foreach (DirectoryInfo d in directories) Console.WriteLine(d.Name); } 8
  9. Application: Find file • Can search file system to find specified files void Find(string filename, DirectoryInfo root, ArrayList results) { search with foreach (FileInfo f in root.GetFiles(filename)) pattern results.Add(f.FullName); recursive call for each foreach (DirectoryInfo d in root.GetDirectories()) subdirectory Find(filename, d, results); } DirectoryInfo root = new DirectoryInfo(@"C:\WINDOWS"); ArrayList results = new ArrayList(); Find("mscoree.dll", root, results); foreach (string s in results) Console.WriteLine(s); 9
  10. Utility classes • Three utility classes also work with files and directories – Path – Directory – File 10
  11. Path • Path provides static methods to manipulate a path string – most do not interact with the file system public sealed class Path { public static bool HasExtension (string path) ... public static string GetExtension (string path) ... public static string ChangeExtension(string path, string extension) ... public static string GetDirectoryName(string path) ... public static string GetFileName (string path) ... public static string GetFileNameWithoutExtension(string path) ... public static readonly char DirectorySeparatorChar; public static readonly char VolumeSeparatorChar; public static readonly char PathSeparator; ... } 11
  12. Application: parse path • Path methods useful to parse path into constituent parts public void Parse(string path) { C:\WINDOWS\system32 string a = Path.GetDirectoryName (path); mscoree.dll string b = Path.GetFileName (path); mscoree string c = Path.GetFileNameWithoutExtension(path); .dll string d = Path.GetExtension (path); C:\ string e = Path.GetPathRoot (path); ... } string path = @"C:\WINDOWS\system32\mscoree.dll"; Parse(path); 12
  13. Directory • Directory has static methods to manipulate directories – most have analogues in DirectoryInfo – several offer unique functionality public sealed class Directory { all drives on system public static string[] GetLogicalDrives() public static string GetCurrentDirectory() current directory public static void SetCurrentDirectory(string path) public static bool Exists (string path) ... public static string[] GetFiles (string path) ... similar methods in public static string[] GetDirectories(string path) ... DirectoryInfo public static void Delete (string path) public static void Move(string source, string dest) ... } 13
  14. Application: examine drives • Can examine all logical drives – use Directory.GetLogicalDrives to get drives – use Directory.Exists to determine if disk in drive all drives string[] drives = Directory.GetLogicalDrives(); foreach (string s in drives) { disk in drive? if (Directory.Exists(s)) ... } 14
  15. File • File has static methods to manipulate files – all have analogues in FileInfo public sealed class File { public static bool Exists(string path) ... similar methods public static void Delete(string path) ... in FileInfo public static void Copy (string source, string dest) ... public static void Move (string source, string dest) ... ... } 15
  16. Choice of file / directory class • Must sometimes decide which class to use – File vs. FileInfo – Directory vs. DirectoryInfo • Static methods – can offer more convenient usage syntax – require permission check on each call • Instance methods – must instantiate object – some permission checks can be done once in constructor 16
  17. File contents manipulation • Classes provided to manipulate file contents – separate classes for text and binary files – filters convert simple types to/from bytes for binary storage – support for disk or memory files 17
  18. Character IO • Classes provided to do character IO – most methods defined in base classes – derived classes specialized for data location and operation TextWriter TextReader StreamWriter StringWriter StreamReader StringReader write to disk write to StringBuilder read from disk read from string 18
  19. StreamWriter • StreamWriter writes characters to text file – open file with one of many constructors – write with overloaded Write / WriteLine methods – close automatically converted to string char, bool, string, using ToString short, int, long, text file float, double, etc. open StreamWriter sw = new StreamWriter("Chores.txt"); int n = 3; sw.WriteLine("Go to pet store"); sw.Write("Feed all "); write sw.Write(n); sw.WriteLine(" cats"); close sw.Close(); 19
  20. StreamReader • StreamReader reads characters from text file – open file with one of many constructors – read characters or strings with Read / ReadLine methods – close can read only char or string char, string text file open StreamReader sr = new StreamReader("Chores.txt"); string s; read while ((s = sr.ReadLine()) != null) Console.WriteLine(s); close sr.Close(); 20
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2