Presentation is loading. Please wait.

Presentation is loading. Please wait.

File Output Writing data out to a file 1. Output to files  StreamWriter : An object in the System.IO namespace that lets you print output to a destination.

Similar presentations


Presentation on theme: "File Output Writing data out to a file 1. Output to files  StreamWriter : An object in the System.IO namespace that lets you print output to a destination."— Presentation transcript:

1 File Output Writing data out to a file 1

2 Output to files  StreamWriter : An object in the System.IO namespace that lets you print output to a destination such as a file.  Any methods you have used on Console.out (such as Write, WriteLine ) will work on a StreamWriter.  Syntax: StreamWriter name = new StreamWriter( " file name " );  Example: StreamWriter output = new StreamWriter( "out.txt" ); output.WriteLine("Hello, file!"); output.WriteLine("This is a second line of output."); output.Dispose();  If you don’t call.Dispose() then C# will not write the data to the file 2

3 Output to files  Example: StreamWriter output = new StreamWriter( "out.txt" ); output.WriteLine("Hello, file!"); output.WriteLine("This is a second line of output."); output.Dispose();  If you don’t call.Dispose() then C# will not write the data to the file  You can use the ‘using’ construct, similar to the input slides: using (StreamWriter output = new StreamWriter("out.txt")) { output.WriteLine("Hello, file!"); output.WriteLine("This is a second line of output."); } // output.Dispose() called here automatically // This is C#-specific  We’ll use the.Dipose() pattern in these slides (so you’re used to it) 3

4 Details about StreamWriter StreamWriter name = new StreamWriter( " file name " );  If the given file does not exist, it is created.  If the given file already exists, it is overwritten.  The output you print appears in a file, not on the console. You will have to open the file with an editor to see it.  Do not open the same file for both reading ( StreamReader ) and writing ( StreamWriter ) at the same time.  You will overwrite your input file with an empty file (0 bytes). 4

5 Console.Out and StreamWriter  The console output object, Console.Out, is a TextWriter.  The StreamWriter class is a subclass of the TextWriter class. TextWriter out1 = Console.Out; TextWriter out2 = new StreamWriter( "data.txt" ); out1.WriteLine("Hello, console!"); // goes to console out2.WriteLine("Hello, file!"); // goes to file out2.Dispose();  A reference to Console.Out can be stored in a TextWriter variable.  Printing to that variable causes console output to appear.  You can pass Console.out to a method as a TextWriter.  Allows a method to send output to the console or a file. 5

6 Hours question  Given a file hours.txt with the following contents: 123 Kim 12.5 8.1 7.6 3.2 456 Eric 4.0 11.6 6.5 2.7 12 789 Stef 8.0 8.0 8.0 8.0 7.5  Task: compute hours worked by each person  Send the output to the file hours_out.txt.  The program will produce no console output.  But the file hours_out.txt will be created with the text: Kim (ID#123) worked 31.4 hours (7.85 hours/day) Eric (ID#456) worked 36.8 hours (7.36 hours/day) Stef (ID#789) worked 39.5 hours (7.9 hours/day)

7 Hours answer public void Slide_8() { char[] delimiters = { ' ', '\t' }; TextReader input = new StreamReader("Files/hours.txt"); StreamWriter output = new StreamWriter("Files/hours_out.txt"); String sLine; while ((sLine = input.ReadLine()) != null) { string[] tokensFromLine = sLine.Split(delimiters, StringSplitOptions.RemoveEmptyEntries); if (tokensFromLine.Length < 3) { Console.WriteLine("Line contained fewer than 3 tokens! {0}", sLine); continue; // get next line } 7

8 Hours answer int ID; if (false == Int32.TryParse(tokensFromLine[0], out ID)) { Console.WriteLine("Line did not start with ID number! {0}", sLine); continue; // get next line } string name = tokensFromLine[1]; double sum = 0.0; int count = 0; for (int i = 2; i < tokensFromLine.Length; i++) { double dNum; if (Double.TryParse(tokensFromLine[i], out dNum)) { sum += dNum; count++; } 8

9 Hours answer if( count == 0 ) // didn't find any hours worked { Console.WriteLine("Line did not find any hours worked! {0}", sLine); continue; // get next line } double average = sum / count; output.WriteLine("{0} (ID#{1}) worked {2} hours ({3} hours/day)", name, ID, sum, average); // If you change output.WriteLine to Console.WriteLine // you’d see the output on the screen } output.Dispose(); } 9

10 Credits  The Chapter 6 slides from Reges and Stepp’s excellent “Building Java Programs” textbook was used as the basis of these slides.  The instructor was given permission by the publisher to use those slides in this course.  Those slides were modified to work in C#.  Java has two methods of interacting with files: token-based and line-based.  C#’s implementation does not have this distinction (which avoids some problems at the cost of requiring more complicated code) 10


Download ppt "File Output Writing data out to a file 1. Output to files  StreamWriter : An object in the System.IO namespace that lets you print output to a destination."

Similar presentations


Ads by Google