TextReader,其派生類:html
TextWriter,其派生類:spring
見https://www.cnblogs.com/springsnow/p/9428695.htmlapi
public StreamReader (System.IO.Stream stream, System.Text.Encoding encoding ); public StreamReader (string path, System.Text.Encoding encoding)
實例:數組
StreamReader srAsciiFromFile = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII); StreamReader srAsciiFromStream = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),System.Text.Encoding.ASCII);
1、使用的實例StreamReader從文件讀取文本 Read(),Peek()服務器
using (StreamReader sr = new StreamReader(path)) { while (sr.Peek() >= 0) { Console.Write((char)sr.Read()); } }
2、調用其ReadAsync()方法以異步方式讀取文件。app
static async Task Main() { await ReadAndDisplayFilesAsync(); } static async Task ReadAndDisplayFilesAsync() { String filename = "C:\\s.xml"; Char[] buffer; using (var sr = new StreamReader(filename)) { buffer = new Char[(int)sr.BaseStream.Length]; await sr.ReadAsync(buffer, 0, (int)sr.BaseStream.Length); } Console.WriteLine(new String(buffer)); }
3、讀取一行字符。ReadLine()異步
using (StreamReader sr = new StreamReader("TestFile.txt")) { string line; // Read and display lines from the file until the end of the file is reached. while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); } }
4、讀取到一個操做中的文件的末尾。ReadToEnd()async
using (StreamReader sr = new StreamReader(path)) { Console.WriteLine(sr.ReadToEnd()); }
public StreamWriter (System.IO.Stream stream, System.Text.Encoding encoding); public StreamWriter (string path, bool append, System.Text.Encoding encoding);
StreamWriter類容許直接將字符和字符串寫入文件ide
//保留文件現有數據,以追加寫入的方式打開d:\file.txt文件 using (StreamWriter sw = new StreamWriter(@"d:\file.txt", true)) { //向文件寫入新字符串,並關閉StreamWriter sw.WriteLine("Another File Operation Method"); }
給定要監視的指定目錄和文件類型,初始化 FileSystemWatcher 類的新實例。函數
public FileSystemWatcher (string path, string filter);
下面的示例建立FileSystemWatcher監視在運行時指定的目錄。 該組件設置爲監視中的更改LastWrite
和LastAccess
時間、 建立、 刪除、 或重命名的目錄中的文本文件。 若是文件是更改、 建立,或刪除,文件的路徑將打印到控制檯。 在一個文件重命名後,舊的和新路徑將打印到控制檯。
static void Main() { Run(); } [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] private static void Run() { // Create a new FileSystemWatcher and set its properties. using (FileSystemWatcher watcher = new FileSystemWatcher()) { watcher.Path = "C:\\aa\\"; // Watch for changes in LastAccess and LastWrite times, and // the renaming of files or directories. watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; // Only watch text files. watcher.Filter = "*.txt"; // Add event handlers. watcher.Changed += OnChanged; watcher.Created += OnChanged; watcher.Deleted += OnChanged; watcher.Renamed += OnRenamed; // Begin watching. watcher.EnableRaisingEvents = true; // Wait for the user to quit the program. Console.WriteLine("Press 'q' to quit the sample."); while (Console.Read() != 'q') ; } } // Define the event handlers. // Specify what is done when a file is changed, created, or deleted. private static void OnChanged(object source, FileSystemEventArgs e) => Console.WriteLine($"File: {e.FullPath} {e.ChangeType}"); // Specify what is done when a file is renamed. private static void OnRenamed(object source, RenamedEventArgs e) => Console.WriteLine($"File: {e.OldFullPath} renamed to {e.FullPath}");
讀寫流的基元數據類型。能夠操做圖像、壓縮文件等二進制文件。不須要一個字節一個字節進行操做,能夠是2個、4個、或8個字節這樣操做。能夠將一個字符或數字按指定數量的字節進行寫入。
using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create))) { writer.Write(1.250F); writer.Write(@"c:\Temp"); writer.Write(10); writer.Write(true); }
每次讀取都回提高流中的當前位置相應數量的字節。下面的代碼示例演示瞭如何存儲和檢索文件中的應用程序設置。
const string fileName = "AppSettings.dat"; float aspectRatio; string tempDirectory; int autoSaveTime; bool showStatusBar; if (File.Exists(fileName)) { using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open))) { aspectRatio = reader.ReadSingle(); tempDirectory = reader.ReadString(); autoSaveTime = reader.ReadInt32(); showStatusBar = reader.ReadBoolean(); } Console.WriteLine("Aspect ratio set to: " + aspectRatio); Console.WriteLine("Temp directory is: " + tempDirectory); Console.WriteLine("Auto save time set to: " + autoSaveTime); Console.WriteLine("Show status bar: " + showStatusBar); }
BinaryReader讀取圖片:
using (FileStream fs = new FileStream("1.jpg", FileMode.Open, FileAccess.Read)) {//將圖片以文件流的形式進行保存 using (BinaryReader br = new BinaryReader(fs)) { byte[] imgBytesIn = br.ReadBytes((int)fs.Length); //將流讀入到字節數組中 br.Close(); } }