CHAPTER 16 536 THE SYSTEM.IO NAMESPACE static (Web hosting domains)

CHAPTER 16 536 THE SYSTEM.IO NAMESPACE static void Main(string[] args) { Console.WriteLine(”***** Fun with StreamWriter / StreamReader *****n”); … // Now read data from file. Console.WriteLine(”Here are your thoughts:n”); StreamReader sr = File.OpenText(”reminders.txt”); string input = null; while ((input = sr.ReadLine()) != null) { Console.WriteLine (input); } } Once you run the program, you will see the character data within Thoughts.txt displayed to the console. Directly Creating StreamWriter/StreamReader Types One of the slightly confusing aspects of working with the types within System.IO is that you can often achieve an identical result using numerous approaches. For example, you have already seen that you can obtain a StreamWriter via the File or FileInfo type using the CreateText() method. In reality, there is yet another way in which you can work with StreamWriters and StreamReaders: create them directly. For example, the current application could be retrofitted as so: static void Main(string[] args) { Console.WriteLine(”***** Fun with StreamWriter / StreamReader *****n”); // Get a StreamWriter and write string data. StreamWriter writer = new StreamWriter(”reminders.txt”); … // Now read data from file. StreamReader sr = new StreamReader(”reminders.txt”); … } Although it can be a bit confusing to see so many seemingly identical approaches to file I/O, keep in mind that the end result is greater flexibility. In any case, now that you have seen how to move character data to and from a given file using the StreamWriter and StreamReader types, you will next examine the role of the StringWriter and StringReader classes. Source Code The StreamWriterReaderApp project is included under the Chapter 16 subdirectory. Working with StringWriters and StringReaders Using the StringWriter and StringReader types, you can treat textual information as a stream of in-memory characters. This can prove helpful when you wish to append character-based information to an underlying buffer. To illustrate, the following example writes a block of string data to a StringWriter object rather than a file on the local hard drive:
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.

Leave a Reply