- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
Writing Text with a StreamWriter in C#
Writing Text with a StreamWriter QR Maker In C# Using Barcode drawer for Visual Studio .NET Control to generate, create QR Code image in .NET applications. www.OnBarcode.comQR Code 2d Barcode Reader In Visual C#.NET Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications. www.OnBarcode.comThe File class offers a CreateText method, which takes the path to the file to create (either relative or absolute, as usual), and creates it for you if it doesn t already exist. If the file is already present, this method overwrites it. Unlike the WriteAllText method, it doesn t write any data initially the newly created file will be empty at first. The method returns an instance of the StreamWriter class, which allows you to write to the file. Example 11-21 shows the code we need to use that. Create USS-128 In C#.NET Using Barcode printer for .NET framework Control to generate, create GS1 128 image in Visual Studio .NET applications. www.OnBarcode.comPaint Barcode In Visual C# Using Barcode generator for .NET framework Control to generate, create Barcode image in VS .NET applications. www.OnBarcode.comprivate static void CreateFile(string fullPath, string p) { using (StreamWriter writer = File.CreateText(fullPath)) { // Use the stream writer here } } Creating Quick Response Code In Visual C#.NET Using Barcode maker for Visual Studio .NET Control to generate, create QR Code JIS X 0510 image in .NET applications. www.OnBarcode.comCode 3/9 Creation In Visual C# Using Barcode printer for .NET framework Control to generate, create Code 3/9 image in Visual Studio .NET applications. www.OnBarcode.comWe re no longer writing the whole file in one big lump, so we need to let the StreamWriter know when we re done. To make life easier for us, StreamWriter implements IDisposable, and closes the underlying file if Dispose is called. This means that we can wrap it in a using block, as Example 11-21 shows, and we can be assured that it will be closed even if an exception is thrown. Code 128C Creation In C# Using Barcode drawer for .NET Control to generate, create Code 128 Code Set B image in .NET applications. www.OnBarcode.comIndustrial 2 Of 5 Creator In C# Using Barcode maker for .NET framework Control to generate, create 2 of 5 Standard image in VS .NET applications. www.OnBarcode.comSo, what is a StreamWriter The first thing to note is that even though this chapter has Stream in the title, this isn t actually a Stream; it s a wrapper around a Stream. It derives from a class called TextWriter, which, as you might guess, is a base for types which write text into things, and a StreamWriter is a TextWriter that writes text into a Stream. TextWriter defines lots of overloads of Write and WriteLine methods, very similar to those we ve been using on Console in all of our examples so far. Quick Response Code Printer In Java Using Barcode encoder for BIRT Control to generate, create QR Code image in BIRT reports applications. www.OnBarcode.comGenerate QR Code In .NET Framework Using Barcode creation for Visual Studio .NET Control to generate, create Quick Response Code image in Visual Studio .NET applications. www.OnBarcode.comIf it is so similar in signature, why doesn t Console derive from Text Writer TextWriter is intended to be used with some underlying resource that needs proper lifetime management, so it implements IDisposable. Our code would be much less readable if we had to wrap every call on Console with a using block, or remember to call Dispose especially as it isn t really necessary. So, why make TextWriter implement IDisposa ble We do that so that our text-writing code can be implemented in terms of this base class, without needing to know exactly what sort of TextWriter we re talking to, and still handle the cleanup properly. Print Code 128A In VS .NET Using Barcode maker for Reporting Service Control to generate, create Code128 image in Reporting Service applications. www.OnBarcode.comScan GS1 - 12 In Visual Basic .NET Using Barcode recognizer for .NET Control to read, scan read, scan image in Visual Studio .NET applications. www.OnBarcode.comThe File class s CreateText method calls a constructor on StreamWriter which opens the newly created file, and makes it ready for us to write; something like this: GS1 DataBar Stacked Generator In Java Using Barcode generation for Java Control to generate, create GS1 DataBar Truncated image in Java applications. www.OnBarcode.comUCC - 12 Generation In .NET Framework Using Barcode creation for .NET framework Control to generate, create UCC - 12 image in .NET applications. www.OnBarcode.comreturn new StreamWriter(fullPath, false); Print UPC-A In Visual Studio .NET Using Barcode generator for Reporting Service Control to generate, create UPC Code image in Reporting Service applications. www.OnBarcode.comScanning European Article Number 13 In Java Using Barcode decoder for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comThere s nothing to stop you from doing this yourself by hand, and there are many situations where you might want to do so; but the helper methods on File tend to make your code smaller, and more readable, so you should consider using those first. We ll look at using Stream Writer (and its partner, StreamReader) in this way later in the chapter, when we re dealing with different sorts of underlying streams. Scanning GTIN - 13 In VB.NET Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.comBarcode Creation In None Using Barcode printer for Online Control to generate, create Barcode image in Online applications. www.OnBarcode.comHang on, though. We ve snuck a second parameter into that constructor. What does that Boolean mean When you create a StreamWriter, you can choose to overwrite any existing file content (the default), or append to what is already there. The second Boolean parameter to the constructor controls that behavior. As it happen, passing false here means we want to overwrite. Decode QR Code JIS X 0510 In Visual C#.NET Using Barcode reader for .NET framework Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.comPDF 417 Reader In None Using Barcode decoder for Software Control to read, scan read, scan image in Software applications. www.OnBarcode.comThis is a great example of why it s better to define nicely named enumerations, rather than controlling this sort of thing with a bool. If the value had not been false, but some mythical value such as OpenBehav ior.Overwrite, we probably wouldn t have needed to explain what it did. C# 4.0 added the ability to use argument names when calling methods, so we could have written new StreamWriter(fullPath, append: false), which improves matters slightly, but doesn t help you when you come across code that hasn t bothered to do that. So, now we can easily complete the implementation of our CreateFile method, as shown in Example 11-22. private static void CreateFile(string fullPath, string p) { using (StreamWriter writer = File.CreateText(fullPath)) { writer.Write(p); } } We just write the string we ve been provided to the file. In this particular application, Example 11-22 isn t an improvement on Example 11-20 we re just writing a single string, so WriteAllText was a better fit. But StreamWriter is an important technique for less trivial scenarios.
|
|