Takes a collection of strings one for each line but

Info icon This preview shows pages 421–423. Sign up to view the full content.

takes a collection of strings, one for each line, but our code in Exam- ple 11-19 prepares content in the form of a single string. So as Example 11-20 shows, we use WriteAllText to write the file out with a single line of code. (In fact, we probably didn’t need to bother putting this code into a separate method. However, this will make it easier for us to illustrate some of the alternatives later.) Example 11-20. Writing a string into a new file private static void CreateFile(string fullPath, string contents) { File.WriteAllText(fullPath, contents); } The path can be either relative or absolute, and the file will be created if it doesn’t already exist, and overwritten if it does. This was pretty straightforward, but there’s one problem with this technique: it requires us to have the entire file contents ready at the point where we want to start writing text. This application already does that, but this won’t always be so. What if your program performs long and complex processing that produces very large volumes of text? Writ- ing the entire file at once like this would involve having the whole thing in memory first. But there’s a slightly more complex alternative that makes it possible to generate gigabytes of text without consuming much memory. Writing Text with a StreamWriter The 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. Example 11-21. Creating a StreamWriter private static void CreateFile(string fullPath, string p) { using (StreamWriter writer = File.CreateText(fullPath)) { // Use the stream writer here } } We’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 imple- ments 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. Writing Text Files | 397
Image of page 421

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

So, 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 sim- ilar to those we’ve been using on Console in all of our examples so far.
Image of page 422
Image of page 423
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '15

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern